2 Replies Latest reply on Aug 20, 2007 6:50 AM by maksimkaszynski

          ajax4jsf can not working with the download files

    icess

      when i add ajax4jsf filter in web.xml file, the download function can not working .

      <display-name>Ajax4jsf Filter</display-name>
      <filter-name>ajax4jsf</filter-name>
      <filter-class>org.ajax4jsf.Filter</filter-class>

      <filter-mapping>
      <filter-name>ajax4jsf</filter-name>
      <servlet-name>Faces Servlet</servlet-name>
      REQUEST
      FORWARD
      INCLUDE
      </filter-mapping>
      the download code:
      String path = "d:\\";
      String name = "llk3.exe"; // the download file name . OK!!
      FacesContext ctx = FacesContext.getCurrentInstance();
      ResponseWriter writer=ctx.getResponseWriter();
      HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
      response.reset();
      response.setContentType("application/x-download");
      response.addHeader("Content-Disposition",
      "attachment; filename=" + name + "");
      try {
      ServletOutputStream os = response.getOutputStream();
      FileInputStream fis = new java.io.FileInputStream(path + name);

      byte[] b = new byte[1024];
      int i = 0;

      while ( (i = fis.read(b,0,b.length)) > 0) {
      os.write(b, 0, i);
      }
      fis.close();
      os.flush();
      os.close();
      }
      catch(IOException ex){
      ex.printStackTrace();
      }


      if remove the ajax4jsf filter , the code can working .

      how i can fix this. any help. thx!

        • 1. Re:       ajax4jsf can not working with the download files
          jmiguel77

          From wich component are you calling this download code ???

          I have something similar; in a <a4j:form> i have a commandLink wich actionListener uses a very similar code for downloading a file

          the thing is that i use an h:commandLink not an a4j:commandLink

          This is my code:

           FacesContext facesContext = FacesContext.getCurrentInstance();
           File file = new File(documentPath);
           BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
          
           byte[] buf = new byte[1024];
           long length = file.length();
          
           if (!facesContext.getResponseComplete()) {
           String fileName = file.getName();
           String contentType = documento.getMimeType();
          
           HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
           response.setContentType(contentType);
           response.setHeader("Content-Disposition","attachment;filename=\"" + fileName + "\"");
          
           ServletOutputStream out = response.getOutputStream();
           response.setContentLength((int)length);
           while ((in != null) && ((length = in.read(buf)) != -1)) {
           out.write(buf, 0, (int)length);
           }
          
           in.close();
           out.close();
          
           facesContext.responseComplete();
          
          


          Hope it helps

          • 2. Re:       ajax4jsf can not working with the download files
            maksimkaszynski

            I agree with jmiguel77.
            You have to call

            facesContext.responseComplete();

            if you send file from faces action.
            Otherwise, you will have components rendered after file-sending code.
            Exception you catch is most likely caused by components trying to render via closed response stream.