Downloading files

The fileDownload component can be used to stream binary contents, such as files to requesting browsers, by wrapping the components with any JSF command component, such as a button or link.

How to do it…

The value of the fileDownload component should be an instance of org.primefaces.model.StreamedContent. The concrete class org.primefaces.model.DefaultStreamedContent could be used in your implementation, which is also suggested by us.

The following is the backing bean implementation for the file download:

public class FileBean implements Serializable {
  private StreamedContent file;

  public FileBean() {
    InputStream stream = this.getClass().
      getResourceAsStream("/chapter7/PFSamplePDF.pdf");
    file = new DefaultStreamedContent(stream,
      "application/pdf", "PFSample.pdf");
  }

  public StreamedContent getFile() {
    return file;
  }

  public StreamedContent getDownloadFile() {
    return downloadFile;
  }
}

The fileDownload component is wrapped by commandButton. The definition is given as follows:

<p:commandButton value="Download" ajax="false">
  <p:fileDownload value="#{fileBean.file}" />
</p:commandButton>

Tip

Since the file download progress is non-AJAX, the ajax attribute for PrimeFaces command components that are used for wrapping the fileDownload component should be set to false.

There's more…

By default, the disposition of the downloadable content will be done with a download dialog box, which is the attachment mode, but setting the contextDisposition attribute to inline will make the browser try to open the file within itself without any prompt.

Note

Content disposition is not part of the HTTP standard, but it's widely adopted by the browsers.

Monitoring download status

File download is a non-AJAX process. So, in order to monitor the status, PrimeFaces provides the client-side monitorDownload method since we cannot use the <p:ajaxStatus> component for monitoring purposes. The method can be bound to an onclick event of a command component, as seen in the following code snippet:

<h:commandLink onclick="PrimeFaces.monitorDownload(showStatus,
  hideStatus)">
  <p:graphicImage
    value="/resources/images/download/fileDownload.png" />
  <p:fileDownload value="#{fileBean.downloadFile}" />
</h:commandLink>

This method will trigger two methods, showStatus and hideStatus, when the download process occurs.

The showStatus and hideStatus are two simple methods for showing and hiding a dialog box component. These methods are described in the following code snippet:

<script type="text/javascript">
  function showStatus() {
    statusDialog').show();
  }
  function hideStatus() {
    PF('statusDialog').hide();
  }
</script>

The definition of the dialog box is given as follows:

<p:dialog modal="true" widgetVar="statusDialog" header="Status"
  draggable="false" closable="false">
  <p:graphicImage value="/resources/images/ajax-loader.gif" />
</p:dialog>

Tip

File download needs a cookie named primefaces.download for handling monitoring purposes, so session cookies should be enabled within the browser for proper usage.

PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on every Servlet 3.x compatible application server, such as JBoss WildFly or Apache TomEE.

The showcase for the recipe is available under http://localhost:8080/pf-cookbook/views/chapter7/fileDownload.jsf.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.28.179