1 Reply Latest reply on Dec 2, 2009 1:59 PM by janvandam

    No 'save as' fileDialog pops up using commandLink

    janvandam

      My problem is to download a file rom the server. When using a commandLink like this:




                  <a:commandLink id="download"
                              value="Download"
                             action="#{outputFileDialogAction.initDownload(outputFile)}"
                         oncomplete="Richfaces.showModalPanel('dialogActionPanel');" 
                   propagation="none"
                           rendered="#{empty from}"
                           reRender="dialogActionPanel"
                        limitToList="true"/>
      




      my backing bean is called and the selected outputFile from my dataTable is handed over to the backing bean. However the code from my backing bean:




          public void executeDownload(OutputFile outputFile) {
               
                FacesContext facesContext = FacesContext.getCurrentInstance();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                PrintWriter writer = new PrintWriter(baos);
                writer.write("content", 0, 7);
                writer.write("\n");
                writer.flush();
               writer.close();
               HttpServletResponse response = ((HttpServletResponse)facesContext.getExternalContext().getResponse());
                response.setContentType("text/plain");
                response.addHeader("Content-disposition", "attachment; filename=\"" + "filename" +"\"");
                ServletOutputStream sos;
                try {
                     sos = response.getOutputStream();
                
                     sos.write(baos.toByteArray());
                     sos.flush();
                     sos.close();
                } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                facesContext.responseComplete();
          }
      



      ... does not result in a 'save as' dialog as expected; instead the data ('content') is written to the browser.


      If on the other hand, I use an s:link:




                  <s:link view="/OutputFileList.xhtml"
                        action="#{outputFileDialogActions.initDownload(outputFile)}" value="Download"
                   propagation="none"
                            id="outputFileList2"
                      rendered="#{empty from}">
                      <s:conversationPropagation type="begin"/>
                  </s:link>
      



      I do get a 'save as' popup as desired, however the outputFile parameter is null, so I am not able to retrieve to data I want to download.


      Can somebody help me?

        • 1. Re: No 'save as' fileDialog pops up using commandLink
          janvandam

          Found the solution myself using


          <s:link>
          



          in combination with a parameter:




                      <s:link view="/OutputFileList.xhtml"
                            action="#{outputFileDialogActions.download}" value="Download"
                       propagation="none"
                                id="outputFileList"
                          rendered="#{empty from}">
                          <s:conversationPropagation type="begin"/>
                          <f:param name="outputFileId" value="${_outputFile.id}" />
                      </s:link>
          



          and a @RequestParameter in the backing bean:


             



              @In
              private EntityManager entityManager;
              
              @RequestParameter
                  private long outputFileId;
          





              
          @Begin(join=true)
          public void download() {
              OutputFile of = entityManager.find(OutputFile.class, outputFileId);
          }
          




          Problem solved.