Jsf File Download

Active1 year ago
Jsf multiple file download

Is there any way of providing a file download from a JSF backing bean action method?I have tried a lot of things. Main problem is that I cannot figure how to get the OutputStream of the response in order to write the file content to. I know how to do it with a Servlet, but this cannot be invoked from a JSF form and requires a new request.

JSF files store JavaScript code command sequences. JSF file format applications. Commands stored in JSF file are used for automating frequently executed tasks in Adobe Fireworks. Such command sequences can, for example, change color or size of given shape. Single JSF file can store virtually unlimited number of such commands. With the help of the FileDownload component it becomes easier to program the download of a static or dynamic file in JSF. You no longer need a servlet to perform these kind of actions. With the help of the very broad useable RemoteCommand component, we can even initiate 2 actions.

How can I get the OutputStream of the response from the current FacesContext?

BalusC

File Download Movie

890k312 gold badges3266 silver badges3296 bronze badges
zmedazmedaFile
1,4207 gold badges29 silver badges54 bronze badges

4 Answers

Introduction

You can get everything through ExternalContext. In JSF 1.x, you can get the raw HttpServletResponse object by ExternalContext#getResponse(). In JSF 2.x, you can use the bunch of new delegate methods like ExternalContext#getResponseOutputStream() without the need to grab the HttpServletResponse from under the JSF hoods.

On the response, you should set the Content-Type header so that the client knows which application to associate with the provided file. And, you should set the Content-Length header so that the client can calculate the download progress, otherwise it will be unknown. And, you should set the Content-Disposition header to attachment if you want a Save As dialog, otherwise the client will attempt to display it inline. Finally just write the file content to the response output stream.

Jsf file download software

Most important part is to call FacesContext#responseComplete() to inform JSF that it should not perform navigation and rendering after you've written the file to the response, otherwise the end of the response will be polluted with the HTML content of the page, or in older JSF versions, you will get an IllegalStateException with a message like getoutputstream() has already been called for this response when the JSF implementation calls getWriter() to render HTML.

Turn off ajax / don't use remote command!

You only need to make sure that the action method is not called by an ajax request, but that it is called by a normal request as you fire with <h:commandLink> and <h:commandButton>. Ajax requests and remote commands are handled by JavaScript which in turn has, due to security reasons, no facilities to force a Save As dialogue with the content of the ajax response.

In case you're using e.g. PrimeFaces <p:commandXxx>, then you need to make sure that you explicitly turn off ajax via ajax='false' attribute. In case you're using ICEfaces, then you need to nest a <f:ajax disabled='true' /> in the command component.

Generic JSF 2.x example

Generic JSF 1.x example

Common static file example

In case you need to stream a static file from the local disk file system, substitute the code as below:

Common dynamic file example

In case you need to stream a dynamically generated file, such as PDF or XLS, then simply provide output there where the API being used expects an OutputStream.

E.g. iText PDF:

E.g. Apache POI HSSF:

Note that you cannot set the content length here. So you need to remove the line to set response content length. This is technically no problem, the only disadvantage is that the enduser will be presented an unknown download progress. In case this is important, then you really need to write to a local (temporary) file first and then provide it as shown in previous chapter.

Utility method

If you're using JSF utility library OmniFaces, then you can use one of the three convenient Faces#sendFile() methods taking either a File, or an InputStream, or a byte[], and specifying whether the file should be downloaded as an attachment (true) or inline (false).

Yes, this code is complete as-is. You don't need to invoke responseComplete() and so on yourself. This method also properly deals with IE-specific headers and UTF-8 filenames. You can find source code here.

Jasper de Vries
9,6805 gold badges38 silver badges75 bronze badges
BalusCBalusC
890k312 gold badges3266 silver badges3296 bronze badges
John MendesJohn Mendes
3492 gold badges5 silver badges15 bronze badges
Koray TugayKoray Tugay
9,67830 gold badges126 silver badges240 bronze badges

What Is Jsf File

here is the complete code snippet http://bharatonjava.wordpress.com/2013/02/01/downloading-file-in-jsf-2/

You may change the file reading logic in case you want file to get generated at runtime.

Jsf Documentation

Bharat SharmaBharat Sharma

Jsf Servlet Download File

Not the answer you're looking for? Browse other questions tagged jsfjsf-2downloadbacking-beans or ask your own question.