1 Reply Latest reply on Jun 22, 2009 3:29 AM by swd847

    downloading file prevents facesmessage to appear

    arnieaustin.arnie.morein.me.com

      I have a Seam 2.1.2 app that generates reports. All output is via PDF files which I'm generating directly by the iText APIs. After generation, the PDF file is downloaded to the workstation and Acrobat Reader takes over.


      The odd thing I cannot get to work right is the appearance of a message ('report completed') if I allow the download to proceed. E.g:



      if ( renderReport() ) {
         facesMessages.add(Severity.INFO, "#{messages['global.reportCompleted']}");
         downloadReportFile();
      } else {
         facesMessages.add(Severity.ERROR, "#{messages['global.reportError']}"); 
      }





      If I comment out the downloadReportFile(); line, the message appears as expected. But if I allow the download, it doesn't. The file downloads just fine and the application continues to operate without issue.


      The download code is pretty straight forward. I got the download method from here I believe, or maybe else where; it doesn't matter.


      But it clearly is interfering with Seam's processing or cache. It is listed below. Is there something I can do before calling it to make sure the message gets processed?


      Thanks!


      public Boolean download() {
           Boolean result = false;
           HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();
           response.setContentType("application/pdf");
           response.addHeader("Content-disposition", "attachment; filename=\"" + fileName +"\"");
      
           //Now we create some variables we will use for writing the file to the response
           int read = 0;
           byte[] bytes = new byte[1024];
      
           //Streams we will use to read, write the file bytes to our response
           FileInputStream fis = null;
           File file = null;                
      
           try {
                ServletOutputStream os = response.getOutputStream();
      
                //First we load the file in our InputStream
               try {
                   file = new File(fileName);
                     fis = new FileInputStream(file);
                    os = response.getOutputStream();
      
                      //While there are still bytes in the file, read them and write them to our OutputStream
                    while((read = fis.read(bytes)) != -1){
                       os.write(bytes,0,read);
                    }
      
                      //Clean resources
                    os.flush();
                    os.close();
                    file.delete();
      
                } catch (FileNotFoundException e) {
                     e.printStackTrace();
                } catch (IOException e) {
                     e.printStackTrace();
                }
                
                os.flush();
                os.close();
                facesContext.responseComplete();
                result = true;
           } catch(Exception e) {
                log.error("\nFailure : " + e.toString() + "\n");
           }
      
           return result;
      }


        • 1. Re: downloading file prevents facesmessage to appear
          swd847

          If you want to see the messages you need two http requests, one to re-render the page and one to download the file. There are a few different ways of completing this, the easiest one is probably to use an embedded invisible iframe to actually handle the download request. No matter what you do it will involve some stuffing around, you have to store the file in the conversation or session context and then use a page action in your iframe view to actually download the file.


          There may be some pre made components to make this easier somewhere, as it is a fairly common task, but I don't know of any off the top of my head.


          Stuart