1 2 Previous Next 17 Replies Latest reply on Apr 28, 2009 9:53 PM by elf

    [Tree] how to download file on nodeSelectListener

    lmk

      Hi,

      I'd like to dowload file on a new windows on nodeSelectListener, the file is opened on the browser..

      this is not reproduced when I do downlaod on h:commandButton, it seems related to ajax requests.

      here a snipped code for download:

      
      response.setContentType("application/octet-stream");
       response.setHeader("Content-Disposition", "attachment; filename=" + file.getName() + "");
       OutputStream out;
       try {
       out = response.getOutputStream();
      
       out.write(file.getContent());
       out.close();
       context.responseComplete();
       } catch (IOException e) {
      
       e.printStackTrace();
       }
      



      best regards!



        • 1. Re: [Tree] how to download file on nodeSelectListener
          nbelaevski

          Hello,

          It is not possible to download file by AJAX request directly. Try redirecting to file.

          • 2. Re: [Tree] how to download file on nodeSelectListener
            lmk

             

            "nbelaevski" wrote:
            Hello,

            It is not possible to download file by AJAX request directly. Try redirecting to file.


            how Can I redirect to file ..?

            thanks

            • 3. Re: [Tree] how to download file on nodeSelectListener
              lmk

               

              "lmk" wrote:
              "nbelaevski" wrote:
              Hello,

              It is not possible to download file by AJAX request directly. Try redirecting to file.


              how Can I redirect to file ..?

              thanks


              I tried :
              <a4j:support event="onselected" action="#{fTPViewerTree.downloadSelectedFile}">
              </a4j:support>


              and after I use redirect on navigation case.
              but nothing happeng, the file is not displayed on the navigator nor downloaded..


              • 4. Re: [Tree] how to download file on nodeSelectListener
                nbelaevski

                Please post action code.

                • 5. Re: [Tree] how to download file on nodeSelectListener
                  lmk

                  here the action code

                  public String downloadSelectedFile() {
                  
                   String res = null;
                   if (getSelectedFile() != null) {
                  
                   FileObject file = getSelectedFile().getFile();
                  
                   FacesContext context = FacesContext.getCurrentInstance();
                   ExternalContext external = context.getExternalContext();
                   HttpServletResponse response = (HttpServletResponse) external.getResponse();
                  
                   response.setContentType("application/octet-stream");
                   response.setHeader("Content-Disposition", "attachment; filename=" + file.getName() + "");
                   OutputStream out;
                   try {
                   out = response.getOutputStream();
                  
                   out.write(file.getContent());
                   out.close();
                   context.responseComplete();
                   } catch (IOException e) {
                  
                   e.printStackTrace();
                   }
                   res = "success";
                   }
                   return res;
                   }
                  


                  navigation :

                  <navigation-rule>
                   <from-view-id>/ftp_files.xhtml</from-view-id>
                   <navigation-case>
                   <from-outcome>success</from-outcome>
                   <to-view-id>/ftp_files.xhtml</to-view-id>
                   <redirect/>
                   </navigation-case>
                   </navigation-rule>
                  


                  regards!


                  • 6. Re: [Tree] how to download file on nodeSelectListener
                    nbelaevski

                    You are calling FacesContext.responseComplete(), that's why redirection does not occur. Please try to use javax.faces.context.ExternalContext.redirect(String).

                    • 7. Re: [Tree] how to download file on nodeSelectListener
                      lmk

                       

                      "nbelaevski" wrote:
                      You are calling FacesContext.responseComplete(), that's why redirection does not occur. Please try to use javax.faces.context.ExternalContext.redirect(String).


                      you mean I do not use JSF navigation case and I do redirect like:

                      external.redirect("/ftp_files.xhtml");

                      ?
                      I'll test and let you know..


                      • 8. Re: [Tree] how to download file on nodeSelectListener
                        lmk

                        It does not work.

                        • 9. Re: [Tree] how to download file on nodeSelectListener
                          lmk

                          I add a commandButton inside treeNode to send non ajaw request but how I can call nodeSelectListener on button click.

                          <rich:treeNode type="file"
                          nodeSelectListener="#{fTPViewerTree.processFileSelection}">
                          
                          
                          <h:outputText value="#{item.name}" styleClass="text_bold" />
                          <h:commandButton action="#{fTPViewerTree.downloadSelectedFile}" value="dowload" >
                          </h:commandButton>
                          
                          </rich:treeNode>


                          regards!

                          • 10. Re: [Tree] how to download file on nodeSelectListener
                            nbelaevski

                            Works perfectly for me:

                            public void onSelect(NodeSelectedEvent event) throws IOException {
                             FacesContext facesContext = FacesContext.getCurrentInstance();
                             facesContext.getExternalContext().redirect("/tree-demo/pages/ajaxTable.jsf");
                             facesContext.responseComplete();
                             }


                            • 11. Re: [Tree] how to download file on nodeSelectListener
                              lmk

                              Still not work for me, did you try to download a file?

                              You advice to not call responseComplete

                              "nbelaevski" wrote:
                              You are calling FacesContext.responseComplete(), that's why redirection does not occur. Please try to use javax.faces.context.ExternalContext.redirect(String).




                              • 12. Re: [Tree] how to download file on nodeSelectListener
                                nbelaevski

                                 

                                "lmk" wrote:
                                Still not work for me, did you try to download a file?
                                It's redirecting to the proper page, I think file will be also downloaded ok.

                                "lmk" wrote:
                                You advice to not call responseComplete
                                "nbelaevski" wrote:
                                You are calling FacesContext.responseComplete(), that's why redirection does not occur. Please try to use javax.faces.context.ExternalContext.redirect(String).

                                That's for the case of using application navigation rules, but action is sending redirect internally, so it's still necessary.

                                • 13. Re: [Tree] how to download file on nodeSelectListener
                                  lmk

                                  Sorry I dont understand what you mean..
                                  I'd like to dowload file, I have byte array I'd put the byte array on the response and return to the page, the file will be downloaded. The is a text file..with data/ini .. extension..

                                  how Can I send no-ajax request from the Tree to have a normal behaviour for download file..?

                                  thanks.

                                  • 14. Re: [Tree] how to download file on nodeSelectListener
                                    nbelaevski

                                    Redirect the request to some URL in your application that will output file contents. You can create phase listener that will render files.

                                    1 2 Previous Next