<af:fileDownloadActionListener>

fileDownloadActionListener file download action listener fileDownloadAction filedownloadaction


The fileDownloadActionListener tag is a declarative way to allow an action source (<commandButton>, <commandLink>, etc.) to programatically send the contents of a file to the user, optionally with a specific content type and filename. Since file downloads must be processed with an ordinary request - not XMLHttp AJAX requests - this tag forces partialSubmit to be false on the parent component, if it supports that attribute. It is also the only supported way to perform file download within a region.

Example:

This example sends a simple "Hello there!" file to the user.

<source>
            <af:commandButton value="Say Hello">
              <af:fileDownloadActionListener filename="hello_txt"
                                        contentType="text/plain; charset=utf-8"
                                        method="#{bean.sayHello}"/>
            </af:commandButton>

</source>

<source>
    public void sayHello(FacesContext context, OutputStream out) throws IOException
    {
      OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
      w.write("Hi there!");
      // The stream is automatically closed, but since we wrapped it,
      // we'd better flush our writer
      w.flush();
    }

</source>

NOTE: A limitation of the fileDownloadActionListener tag exists in Internet Explorer if the filename is multibyte, like Japanese characters. If the filename is multibyte and does not contain an ascii extension (e.g., .txt or .doc), then the filename will not display correctly in the file download box.

Attributes

Name Type Supports EL? Description
contentType String Yes the MIME type of the file, for example text/plain, text/csv, application/pdf, etc.
filename String Yes the proposed filename for the object. When set, a "Save File" dialog will typically be displayed, though this is ultimately up to the browser. If not set, the content will typically be displayed inline in the browser if possible.
method MethodExpression Only EL the method that will be used to download the file contents. The method takes two arguments, a FacesContext and an OutputStream. The OutputStream will be automatically closed, so the sole responsibility of this method is to write all bytes to the OutputStream.