2 Replies Latest reply on Jan 20, 2012 5:32 AM by Aniket Murtarkar

    how to Download / open file using richfaces

    Aniket Murtarkar Newbie

      Hello,

       

      I am using richfaces 4.1 M4 along with hibernate.

       

      I am uploading pdf file using  <rich:fileUpload> and I am saving data to mysql database in byte array format (byte []).

      I am retriving that data also.

       

      but I have not created any copy of perticular file and stored it in any server location. It is only in database only.

       

      but now I want make available such pdf files to download or 'open and save' either option.

      I am unable to do that so can any one help me to do so.

       

      Regards,

      Aniket

        • 1. Re: how to Download / open file using richfaces
          Vitaliy Pavlov Novice

          Hi, Aniket Murtarkar

           

          I think that code snippet will be help for you

           

          <h:commandLink actionListener="#{reportBean.downloadFile}"/>


           

          public void downloadFile(ActionEvent event) {

           

                  FacesContext context = FacesContext.getCurrentInstance();

                  HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

           

                  int read = 0;

           

                  response.setContentType("application/pdf");

                  response.setHeader("Content-Disposition", "attachment;filename=\"" + "file.pdf" + "\"");

           

                  try {

                      FileInputStream fis = new FileInputStream("file.pdf");

                      OutputStream os = null;

                      byte[] bytes1 = new byte[1024];

           

                      os = response.getOutputStream();

           

                      while ((read = fis.read(bytes1)) != -1) {

                          os.write(bytes1, 0, read);

                      }

           

                      os.flush();

                      os.close();

                      fis.close();

                  } catch (Exception e) {

                      e.printStackTrace();

                      addErrMessage(e);

                  }

                  FacesContext.getCurrentInstance().responseComplete();

              }

           

          Regards, Vitaliy

          • 2. Re: how to Download / open file using richfaces
            Aniket Murtarkar Newbie

            Thank you Vitaliy,

             

            It realy helped me to solve the problem.

            only thing I needed to do is that... I have to create a new file and add all data from database and then given it to response object as given by you.

             

            here is the added code.

             

                  BufferedOutputStream bos = null;

                  File file = new File(fileName);

                 

                  FileOutputStream fos = new FileOutputStream(file);

                  bos = new BufferedOutputStream(fos);

                  bos.write(cmdObj.getData());

             

            FileInputStream fis = new FileInputStream(fileName);

             

            and rest as per the your code.

             

             

            Thanks alot for this great help.