5 Replies Latest reply on Mar 9, 2009 3:31 AM by judovana

    get file

    judovana

      hi.
      I have an been under JSF (now RF) which redirect me

       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext ec = facesContext.getExternalContext();
       ec.dispatch("../download");
      

      to servlet, which returned me FILE by this code:

       byte[] bytes = str.getBytes("UTF-8");
       InputStream input = new ByteArrayInputStream(bytes);
      
      int length = 0;
      ServletOutputStream op = response.getOutputStream();
      ServletContext context = getServletConfig().getServletContext();
      String mimetype = context.getMimeType( "pokus.xml" );
      
      response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
      response.setContentLength( (int)bytes.length );
      response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
      
      
       byte[] bbuf = new byte[bytes.length]; //here is my generated xml
      
      DataInputStream in = new DataInputStream(input);
      
      while ((in != null) && ((length = in.read(bbuf)) != -1))
      {op.write(bbuf,0,length);}
      
      in.close();
      op.flush();
      op.close();
      
      


      But when I swapped poor JSF for RF i stoped working with error message Ajax4JSF own this stream (sorry not copy paste, i do not have this message anymore). So now i cannot return the file.
      I swapped this code to save those bytes to tmp file and redirect to this file. But it is noto so user friendlyas first solution.
      On RF pages is eritten that existing JSF project can use them without loosing it's functionality;) Have RF any similar solution to return files instead of page?





        • 1. Re: get file
          nbelaevski

          Hello,

          1. It's not a redirect, but forward. How do you call this bean method: by method or by some other means?

          2. I do not see FacesContext#responseComplete() call in the code posted, do you have it somewhere?

          • 2. Re: get file
            nbelaevski

            Got typo in my post, fixed:

            "nbelaevski" wrote:
            Hello,

            1. It's not a redirect, but forward. How do you call this bean method: by action or by some other means?

            2. I do not see FacesContext#responseComplete() call in the code posted, do you have it somewhere?


            • 3. Re: get file
              judovana

               

              "nbelaevski" wrote:
              Got typo in my post, fixed:

              Hello,

              1. It's not a redirect, but forward. How do you call this bean method: by action or by some other means?

              2. I do not see FacesContext#responseComplete() call in the code posted, do you have it somewhere?


              1)In jsf-page i have button in the form and sending this form.
               <h:commandButton id="downlaodTrip" action="#{DownlaodBean.register_action}" value="#{msgs.form_download_trip}" />
              


              then in bean
               @TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
               public String register_action() {
               try {
              
               FacesContext facesContext = FacesContext.getCurrentInstance();
               ExternalContext ec = facesContext.getExternalContext();
              ....
              

              so by action.

              but..:)
              2)FacesContext#responseComplete() i have NOWHERE. It should be somewhere O:-)

              • 4. Re: get file
                nbelaevski

                Please try to add FacesContext#responseComplete() just before dispatching to "../download" context.

                • 5. Re: get file
                  judovana

                  Hi! thanx a lot sir. Working