3 Replies Latest reply on Mar 7, 2007 10:03 AM by Jens Weintraut

    File(data) Download problem...

    Waheed murad Newbie

      i am using seam-gen. i am trying to dowload a file or data from server as file. my code is

      FACELETS CODE:
      <h:commandButton value="donwload file" actionListener="#{ERegReportHandlerAction.downloadReportFile}"/>

      Sessionbean Function:

      public void downloadReportFile(){

      ExternalContext exContext=FacesContext.getCurrentInstance().getExternalContext();
      javax.servlet.http.HttpServletResponse response = ((javax.servlet.http.HttpServletResponse)exContext.getResponse());

      //response.setContentType("application/x-download");
      response.setContentType("text/plain");
      response.setHeader("Content-Disposition", "attachment; filename=" + "test.csv");


      try{
      OutputStream out = response.getOutputStream( );

      byte[] arr=("This is the file data.").getBytes();
      response.setContentLength(arr.length);
      out.write(arr);
      out.close();
      }catch(Exception e){
      System.out.println("Error: " + e.getMessage() );
      }

      }

      --------------------------------------------------------------------
      file is downloaded but when i open it, i have the contents written by me(i.e This is the file data) plus the whole html page source code from where i pressed the download button.
      any help regarding this plz.

        • 1. Re: File(data) Download problem...
          Daniel Young Novice

          Hi,

          I'm doing something similar to you for exporting jasper reports (JasperPrint) objects to various output formats.

          I think perhaps you are missing the call to FacesContext.responseComplete(), but I will show you all of my code, because there are also various flush() and close() calls in there which I found were necessary...

           final HttpServletResponse response =
           (HttpServletResponse) context.getExternalContext().getResponse();
           final OutputStream responseStream = response.getOutputStream();
           response.setContentLength(stream.size());
           response.setContentType(contentTypes.get(outputType));
           if (outputType != ReportOutputType.HTML) {
           response.setHeader("Content-Disposition", "attachment; filename=report." + outputType);
           }
           responseStream.write(stream.toByteArray());
           responseStream.flush();
           responseStream.close();
           response.flushBuffer();
           context.responseComplete();
          



          • 2. Re: File(data) Download problem...
            Jens Weintraut Apprentice

            Hi jazir,

            I tried your code in my application where I get a file out of a database blob and want to send it to the browser.

            But I ran into a NullPointerException, since I don't get a HttpServletResponse object from the FacesContext object. Could it be that I inject the wrong context?

            @In
            private FacesContext context;


            Or is it because I use ICEfaces, that I can't get a HttpServletResponse? Has somebody implemented a file download with ICEfaces?

            Thanks in advance
            Newlukai

            • 3. Re: File(data) Download problem...
              Jens Weintraut Apprentice

              I got it. Almost. I'm stuck on a Seam/ICEfaces integration problem with embedded JavaScript.