3 Replies Latest reply on Oct 30, 2008 4:38 AM by gizola

    Force the browser for download prompt

      Hi,

      in the action method of commandlink(a download link),I am tring to stream the pdf file back to the browser.but browser si not showing the download dailoge box. How can i force the browser to ask for the download prompt.
      May be this is not a new problem for you guys, please help me out in this issue.

      My code looks like this.

      int length = 0;
      ServletOutputStream op = ((HttpServletResponse) context.getExternalContext().getResponse()).getOutputStream();
      ServletContext servcontext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
      String mimetype = servcontext.getMimeType(filename);
      HttpServletResponse resp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
      resp.setContentType((mimetype != null) ? mimetype : "application/pdf");
      String actualFileName = "C:\\Uploadfile\\JMS.pdf";
      resp.setContentType("application/x-download");
      resp.setHeader("Content-Disposition", "attachment; filename=" + actualFileName);
      File f = new File(actualFileName);
      resp.setContentLength((int) f.length());
      System.out.println("file length----" +f.length());
      byte[] bbuf = new byte[10240000];
      DataInputStream in = new DataInputStream(new FileInputStream(f));
      while ((in != null) && ((length = in.read(bbuf)) != -1)) {
      op.write(bbuf, 0, length);
      }

      [img][/img]

        • 1. Re: Force the browser for download prompt
          gizola


          Try to set following:

          response.setContentType("application/x-download");
          response.setHeader("Content-Disposition", "attachment; filename=file.pdf");
          response.setHeader("Content-Transfer-Encoding", "application/octet-stream");
          


          This normally forces the browser to save the file rather than open it, like if you set content type to pdf or image or doc, or wathever the browser plugins supports.

          Regards,

          Gizola

          • 2. Re: Force the browser for download prompt

            i tried it, but no luck.

            • 3. Re: Force the browser for download prompt
              gizola

              This is working for me:

               FacesContext ctx = FacesContext.getCurrentInstance();
              
               HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
               // file should be downloaded without display
               response.setContentType("application/x-download");
               response.setHeader("Content-Disposition", "attachment; filename=file.pdf");
               response.setHeader("Content-Transfer-Encoding", "application/octet-stream");
              // alternatively you can try:
              // response.setHeader("Content-Transfer-Encoding", "binary");
              
               FileInputStream fis = null;
              
               try
               {
               fis = new FileInputStream("e:\\file.pdf");
               }
               catch (FileNotFoundException e2)
               {
               // TODO Auto-generated catch block
               e2.printStackTrace();
               }
              
               try
               {
               response.setContentLength(fis.available());
               }
               catch (Exception e1)
               {
               // TODO Auto-generated catch block
               e1.printStackTrace();
               }
              
               OutputStream out = null;
              
               try
               {
               out = response.getOutputStream();
               }
               catch (IOException e)
               {
               // TODO
               e.printStackTrace();
               // no error message shown to the user. Consider adding a Faces
               // Message
               }
              
               try
               {
               byte b[] = new byte[fis.available()];
               fis.read(b);
               out.write(b);
               }
               catch (IOException e)
               {
               // TODO Auto-generated catch block
               e.printStackTrace();
               }
               catch (Exception e)
               {
               // TODO Auto-generated catch block
               e.printStackTrace();
               }
              
               ctx.responseComplete(); // this is important to call !!!