views:

33

answers:

1

Hello

which is the right way to download a file using JSF?, just putting a link to the file ?? in that case how do i get the file URL??

i have seen one example using BufferedInputStream:

http://www.winstonprakash.com/articles/jsf/file_download_link.htm

What are the differences?

Thanks

+3  A: 

If it's a simple file, just place in public webcontent (there where you put your static and JSF files) and create a link.

<h:outputLink value="/files/file.ext">link</h:outputLink>

The servletcontainer will worry about applying the correct headers.

If it's located outside the public webcontent for some specific reasons (e.g. in a fixed path at server machine, or in a database), then create a servlet which gets an InputStream of it and writes it to the OutputStream of the response along at least the Content-Type, Content-Disposition and Content-Length headers. You can find here a simple kickoff example. Also that can simply be linked on the servlet's url-pattern.

If it's to be dynamically generated and depending on the JSF specific request parameters, then you can also do so in a managed bean action which is bound by h:commandLink or h:commandButton, but you only need to ensure that you call FacesContext#responseComplete() at end of bean's action method to prevent JSF from taking the navigation in hands. The same kind of servlet code can be reused to stream the file. You can find a kickoff example in this answer.

BalusC
O.o Thx BalusCI couldn't connect in all the weekend, but i have to thank you for all your help =DI really appreciate
ErVeY
You're welcome.
BalusC