FileDownload is used to stream binary contents like files stored in database to the client. FileDownload
is used by attaching it to any JSF command component like button or a link. Additionally presentation
of download can be configured with the contentDisposition attribute that takes either "attachment" or "inline" as a value.
Source
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">
<p:graphicImage value="/design/ajaxloadingbar.gif" />
</p:dialog>
<h:form id="form">
<p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"
icon="ui-icon-arrowthichk-s">
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
</h:form>
<script type="text/javascript">
function start() {
statusDialog.show();
}
function stop() {
statusDialog.hide();
}
</script>
package org.primefaces.examples.view;
import java.io.InputStream;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
public class FileDownloadController {
private StreamedContent file;
public FileDownloadController() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
}
public StreamedContent getFile() {
return file;
}
}