3 Replies Latest reply on Nov 22, 2006 5:38 AM by Reinhard Behrens

    Seam+JSF - File Download through HttpServletResponse problem

    Reinhard Behrens Newbie

      Hi,

      I reference my external context as per http://www.jboss.org/index.html?module=bb&op=viewtopic&t=94163
      but it does not function the way I thought.

      If I create the PDF argument for my action function from a DataModelSelection, then the action should prompt the user to either download or view the test pdf1.pdf when clicked.

      The file reading and output works fine, just not to the ServletOutputStream. It returns "Success" without any errors or Exceptions on my JBoss
      console output.

      Am I missing something? Does the function return value have something to do with it, pageflow etc... ?

      Any help would be appreciated.

      The code :

      @Stateful
      @Name("downloadpdf")
      @Scope(ScopeType.SESSION)
      public class DownloadPDFAction implements DownloadPDF{
      
       @PersistenceContext(type=PersistenceContextType.EXTENDED)
       private EntityManager em;
      
       @In(value="#{facesContext.externalContext}")
       private ExternalContext extCtx;
      
       @In(required=false)
       private PDF pdf;
      
       public String download(PDF pdf)
       {
       String filePath = null;
       String fileName = null;
       int read = 0;
       byte[] bytes = new byte[1000];
      
       HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();
       response.setContentType("application/pdf");
       response.addHeader("Content-disposition", "attachment; filename=\"" + fileName +"\"");
      
       filePath = "C:\\";
       fileName = "pdf1.pdf";
      
       try
       {
       ServletOutputStream os = response.getOutputStream();
       File file = new File(filePath,fileName);
       FileInputStream fis = new FileInputStream(file);
      
       while((read = fis.read(bytes)) != -1)
       {
       os.write(bytes,0,read);
       }
      
       os.flush();
       os.close();
      
       System.out.println("\nSuccess\n");
       }
       catch(Exception e)
       {
       System.out.println("\nFailure : " + e.toString() + "\n");
       }
      
       return "main";
       }
      
       @Destroy @Remove
       public void destroy(){}
      }
      


      The JSF :
      <h:dataTable value="#{pdfs}" var="d_map" rendered="#{pdfs.rowCount > 0}">
       <h:column>
       <s:link value="Document : #{d_map.name}" action="#{downloadpdf.download(d_map)}"/><img width="16" height="17" src="img/acrobat.jpg" />
       </h:column>
      </h:dataTable>