6 Replies Latest reply on Apr 30, 2008 2:01 AM by Shane Bryzak

    How to initiate file download

    Todd Sproule Newbie

      In my Seam 2.0.1 app I've created an in memory Excel file (as stream of bytes).  How do I go about initiating a file download via the browser from an action button so that browser does 'file download warning/where do you want to put it'? What sort of HTML/JSF do I serve up for this? 


      I know this is probably easy but I can't find documentation that tells me how to do this.


      Thanks
      Todd

        • 1. Re: How to initiate file download
          Siarhei Dudzin Apprentice

          meta code:



          /* your action */
          public String download() {
                    HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();
                    response.setContentType("application/octet-stream");
                    response.addHeader("Content-Disposition","attachment;filename=" + FILE_NAME);
          
                    PrintWriter writer = response.getWriter();
          
                     /* DO YOUR THING (like writer.write) */
          
                    // finish
                    writer.flush();
                    facesContext.responseComplete();
                    writer.close();
          
                    return "result;
          
          }


          • 2. Re: How to initiate file download
            yuval valim Newbie

            i have mainly the same problem i m also using a seam action but i want to open the in memory file (stream of bytes) in the browser  i m actually  getting this exception
            java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
                 at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:226)
            and thanks in advance

            • 3. Re: How to initiate file download
              Shane Bryzak Master

              You could implement it as a resource (by extending org.jboss.seam.web.AbstractResource).  Take a look at GraphicImageResource, Remoting or CaptchaImage for examples.

              • 4. Re: How to initiate file download
                Todd Sproule Newbie

                Thanks for the pointer Siarhei.  Exactly what I needed.


                I got the HttpServletResponse through FacesContext - here is a code fragment that I ended up using:




                public void downloadExtract() throws MobileSenseException, IOException {
                          
                     byte[] excelExtract;
                          
                     try {
                          excelExtract = excelExportServices.CreateDownload(walletAccount,
                                         getMinDateTime(), getMaxDateTime());
                          
                          HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
                          
                          response.setContentType("application/octet-stream");
                          response.addHeader("Content-Disposition","attachment;filename=" +
                                         makeExtractFileName(getMinDateTime(),getMaxDateTime()));
                               
                          response.setContentLength(excelExtract.length);
                               
                          OutputStream writer = response.getOutputStream();
                               
                          writer.write(excelExtract);
                               
                          writer.flush();
                          writer.close();
                          
                          FacesContext.getCurrentInstance().responseComplete();               
                          
                     } catch (ExtractEmptyException e) {
                          facesMessages.add("There is nothing to export with the selected dates");
                     } 
                          
                }


                • 5. Re: How to initiate file download
                  yuval valim Newbie

                  i mainly used the same source code but it didn t succeed i m using facelets as my view handler(declared in faces config) i used faces config the same way but i still get the above exception is there any workaround! using a seam AbstractResource may  prevent me from injecting parameters from the users input to generate the in memory excel file 

                  • 6. Re: How to initiate file download
                    Shane Bryzak Master

                    A Resource still provides access to the HttpServletRequest object, so you can still read any parameters that are passed in.