2 Replies Latest reply on Oct 21, 2009 3:25 AM by Olli H.

    Downloaddialog for bytesstream !!!

    Olli H. Newbie

      Hey,

      i have some filecontent in an bytearray and i want to display the file save dialog of the browser. But the dialog never appears, instead of that the whole byte output fills one new page of the browser.

      Does anyone have an working example for that problem or an idea what i have to change ??

      HtmlAjaxCommandButton b = new HtmlAjaxCommandButton();
      IdUtils.assignId(b);
      b.setValueExpression(CommonConstants.ATTRIBUTE_VALUE, ViewUtil.createValueExpression("#{items.fields['"+entiyMetaFieldVO.getField()+"'].value}"));
      b.setActionListener(FacesContext.getCurrentInstance().getApplication().
       createMethodBinding("#{download.performDownload}", new Class[] {ActionEvent.class}));


      And in my backing bean

      FacesContext ctx = FacesContext.getCurrentInstance();
      HttpServletResponse resp = (HttpServletResponse) ctx.getExternalContext().getResponse();
      
      resp.reset();
      resp.setContentLength(file.getContents().length);
      resp.setContentType("application/pdf");
      resp.setHeader("pragma", "no-cache");
      resp.setHeader("Cache-control","no-cache, no-store, must-revalidate") ;
      resp.setHeader("Content-Disposition", "attachment;filename=\"" + file.getFilename() ) ;
      
      ServletOutputStream out = resp.getOutputStream();
      BufferedOutputStream output = new BufferedOutputStream(out);
       try {
       output.write(file.getContents());
       output.flush();
       ctx.responseComplete();
       } catch (IOException e) {
       e.printStackTrace();
       }


      the bean is in session scope and the file.getContents() method returns the byte array!

      Regards,
      Kalomo