8 Replies Latest reply on Jan 26, 2009 3:44 AM by Dongyao Zhao

    How to open or download Excel file from server in richfaces

    Dongyao Zhao Novice

      In my appliction the user may export a data table with Excel format, the excel file is created on the server and directly through servlet send back to user. But i don not know, how to do it. I have written the outputstream, aber i don't get the dialog" open or download" of Firefox. Somebody sags, that firefox don't support the download of Excel file, but with pdf file, it does not work too.

      private static void downloadFile(String strfileName) {
       try {
       FacesContext context = FacesContext.getCurrentInstance();
       // get ServletContext object
       ServletContext servletContext = (ServletContext) context
       .getExternalContext().getContext();
       // get the absolute path of the file
       String excelName = servletContext.getRealPath("/UploadFile") + "/"
       + strfileName;
       File exportFile = new File(excelName);
       HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext
       .getCurrentInstance().getExternalContext().getResponse();
       ServletOutputStream servletOutputStream = httpServletResponse
       .getOutputStream();
       httpServletResponse.setHeader("Content-disposition",
       "attachment; filename=" + strfileName);
       httpServletResponse.setContentLength((int) exportFile.length());
       httpServletResponse.setContentType("application/x-download");
      
       byte[] b = new byte[1024];
       int i = 0;
       FileInputStream fis = new java.io.FileInputStream(exportFile);
       while ((i = fis.read(b)) > 0) {
       servletOutputStream.write(b, 0, i);
       }
       } catch (IOException e) {
       e.printStackTrace();
       }
       FacesContext.getCurrentInstance().responseComplete();
       }
      

      alternative:With h:outputLink, eg. a pdf file can be opened with new tab of firefox. I want to do as it too. Without h:outputLink, only with the servlet outputstream.

      Hat anybody any ideas. Thanks in advance.