Uploaded image for project: 'RichFaces'
  1. RichFaces
  2. RF-13647

File Download throws exception when used with a commandButton of type="button"

    Details

    • Steps to Reproduce:
      Hide

      Here's my code from xhtml

      <h:commandLink action="#{campaignBean.campaignDownload()}"  value="#{ifn:translate('Excel File')}"/>
      

      Backing bean code

       public void campaignDownload() throws Exception {
       
              File f= new File(logDir + fileName);
              /* This is an Omnifaces Utility which will simply gives the file to browser. I tried that but it didn't work either
              	 The same code works fine in 4.3.5 Final and 4.3.4 Final.
              	 http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean
              */
              //Faces.sendFile(f,true);
              
              
              try {
                  FacesContext context = FacesContext.getCurrentInstance();
                  
                  /* Code that reads content from file and store it in object */
                  IBinaryContentProvider binaryContentProvider = dfAction.getBinaryContentProvider();
       
                  HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
                  response.setContentType(binaryContentProvider.getContentMIMEType());
                  response.setHeader("Content-Disposition", "attachment; filename="
                          + binaryContentProvider.getDownloadFilename());
                  ServletOutputStream ouputStream = response.getOutputStream();
                  while (binaryContentProvider.hasMoreChunks()) {
                      byte[] bytes = binaryContentProvider.getNextChunk();
                      // it is ok to have 0 bytes
                      if (bytes != null && bytes.length > 0) {
                          ouputStream.write(bytes);
                      }
                  }
              } catch (Exception ex) {
                  logger.error(Level.SEVERE, "Exception : " + ex);
              } finally {
                  FacesContext.getCurrentInstance().responseComplete();
              }
          }
      

      Show
      Here's my code from xhtml <h:commandLink action="#{campaignBean.campaignDownload()}" value="#{ifn:translate('Excel File')}"/> Backing bean code public void campaignDownload() throws Exception {   File f= new File(logDir + fileName); /* This is an Omnifaces Utility which will simply gives the file to browser. I tried that but it didn't work either The same code works fine in 4.3.5 Final and 4.3.4 Final. http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean */ //Faces.sendFile(f,true); try { FacesContext context = FacesContext.getCurrentInstance(); /* Code that reads content from file and store it in object */ IBinaryContentProvider binaryContentProvider = dfAction.getBinaryContentProvider();   HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType(binaryContentProvider.getContentMIMEType()); response.setHeader("Content-Disposition", "attachment; filename=" + binaryContentProvider.getDownloadFilename()); ServletOutputStream ouputStream = response.getOutputStream(); while (binaryContentProvider.hasMoreChunks()) { byte[] bytes = binaryContentProvider.getNextChunk(); // it is ok to have 0 bytes if (bytes != null && bytes.length > 0) { ouputStream.write(bytes); } } } catch (Exception ex) { logger.error(Level.SEVERE, "Exception : " + ex); } finally { FacesContext.getCurrentInstance().responseComplete(); } }

      Description

      File download action throws below exception.

      ]] Root cause of ServletException.
      java.lang.NullPointerException
              at org.richfaces.renderkit.AjaxCommandRendererBase.isSubmitted(AjaxCommandRendererBase.java:83)
              at org.richfaces.renderkit.AjaxCommandRendererBase.doDecode(AjaxCommandRendererBase.java:59)
              at org.richfaces.renderkit.RendererBase.decode(RendererBase.java:80)
              at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:789)
              at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1183)
              Truncated. see log file for complete stacktrace
      

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  michpetrov Michal Petrov
                  Reporter:
                  santoshvarma santhosh siravuri
                • Votes:
                  1 Vote for this issue
                  Watchers:
                  4 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: