8 Replies Latest reply on Jul 19, 2007 3:39 AM by David Sinaga

    Usage for downloading uploaded file

    Humber Newbie

      Hi people! I have a simple problem but i'm a little lost here.

      I use

      <s:fileUpload data="#{documentHome.instance.file}"
       fileName="#{documentHome.instance.name}"
       accept="" />
      

      to upload a file but i don't know how to make a link to the resource.
      The file can be a pdf, a txt, doc, any binay file.
      How can i create a link to download this file?

      Thanks!!

        • 1. Re: Usage for downloading uploaded file
          Thomas Barry Newbie

          You have to write a servlet to do this. I think the seamspace app provides a good example.

          • 2. Re: Usage for downloading uploaded file
            David Sinaga Newbie

             

            "twocoasttb" wrote:
            You have to write a servlet to do this. I think the seamspace app provides a good example.


            hi,
            could you plz give some more infos ?
            i can't find it in seamspace examples

            thx

            • 3. Re: Usage for downloading uploaded file
              Carlos Zaniolo Newbie

              Hi,

              try this:

              <s:link value="Donwload #{textm.name}" rendered="#{! empty textm.text}" action="#{textmcontroller.download}" />


              public void download() {
               if (textm.getText() != null) {
               byte[] file = loadFile();
               String name = getName();
               String type = getContentType();
               MyUtil.download(file, name, type);
               }
               }



              public static void download(byte[] file, String name, String type) {
               FacesContext facesContext = (FacesContext) Component.getInstance("facesContext");
               if (!facesContext.getResponseComplete()) {
               HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
               response.setContentType(type);
               response.setContentLength(file.length);
               response.setHeader("Content-disposition","attachment; filename=" + name);
               ServletOutputStream out;
               try {
               out = response.getOutputStream();
               out.write(file);
               out.flush();
               } catch (IOException e) {
               //TODO: something
               }
               facesContext.responseComplete();
               }
               }


              • 4. Re: Usage for downloading uploaded file
                Carlos Zaniolo Newbie

                But it wold be usefull to have in Seam:
                <s:filedownload name="#{name}" contentType="#{type}" file="#{file}" />

                • 5. Re: Usage for downloading uploaded file
                  David Sinaga Newbie

                  Hi carlos,
                  thx for your responds.
                  i still don't understand, which one is your textmcontroller java code?
                  thx in advance

                  • 6. Re: Usage for downloading uploaded file
                    Carlos Zaniolo Newbie

                    Sorry,

                    public void download()

                    is from textmcontroller.

                    The other is my download helper method and is the important one...

                    • 7. Re: Usage for downloading uploaded file
                      David Sinaga Newbie

                       

                      "carloszaniolo" wrote:
                      Sorry,

                      public void download()

                      is from textmcontroller.

                      The other is my download helper method and is the important one...


                      thank you sir....it works...

                      but the downloaded file type is undefined, so that you can't open it.
                      the file is converted from blob into byte.
                      the size is correct.
                      well i think it's just a convert problem..
                      i'm still working on it and try to fix it.
                      i let you know


                      • 8. Re: Usage for downloading uploaded file
                        David Sinaga Newbie

                         

                        "tino4" wrote:
                        "carloszaniolo" wrote:
                        Sorry,

                        public void download()

                        is from textmcontroller.

                        The other is my download helper method and is the important one...


                        thank you sir....it works...

                        but the downloaded file type is undefined, so that you can't open it.
                        the file is converted from blob into byte.
                        the size is correct.
                        well i think it's just a convert problem..
                        i'm still working on it and try to fix it.
                        i let you know



                        Hi Carloszaniolo,
                        it's working now. now i can open the downloaded uploaded file.
                        the problem like i said was the convert itself.
                        well actually you don't need to convert at all :)

                        thank you sir