<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Provide file download in a Html user dialog?</title><link>https://answers.axonivy.com/questions/25/provide-file-download-in-a-html-user-dialog</link><description>&lt;p&gt;I want to provide one or more files for my users to download it from a user dialog in JSF, how can I do that?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/25/provide-file-download-in-a-html-user-dialog" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 09 Oct 2013 12:39:38 -0400</lastBuildDate><item><title>Answer by SupportIvyTeam</title><link>https://answers.axonivy.com/questions/25/provide-file-download-in-a-html-user-dialog/26</link><description>&lt;p&gt;&lt;a href="http://www.primefaces.org/"&gt;PrimeFaces&lt;/a&gt; offers the &lt;code&gt;p:fileDownload&lt;/code&gt; widget for this purpose, it allows to use &lt;code&gt;(File)InputStream&lt;/code&gt; for download. &lt;/p&gt;
&lt;p&gt;You need a &lt;code&gt;p:commandButton&lt;/code&gt; (or alternatively a &lt;code&gt;p:commandLink&lt;/code&gt;) to work with the &lt;code&gt;p:fileDownload&lt;/code&gt;. It is important to set the attribute &lt;code&gt;ajax&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; as files cannot be downloaded by AJAX.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;p:commandButton value="Download" actionListener="#{logic.download}" ajax="false"&amp;gt;
        &amp;lt;p:fileDownload value="#{data.file}"&amp;gt;&amp;lt;/p:fileDownload&amp;gt;
&amp;lt;/p:commandButton&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The attribute &lt;code&gt;value&lt;/code&gt; points to a member of the process data class that must have the type &lt;code&gt;org.primefaces.model.StreamedContent&lt;/code&gt;. This object will be the one downloaded. In the code snippet below you can see how you can setup this object in the logic of the Html User Dialog.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import org.primefaces.model.DefaultStreamedContent;
import java.io.FileInputStream;

// create temp file and add some content
File file = new File("mytemp." + System.currentTimeMillis() + ".txt", true);
file.createNewFile();
file.write("Hallo " + System.currentTimeMillis());

// set file to download
FileInputStream fis = new FileInputStream(file.getJavaFile());
in.file = new DefaultStreamedContent(fis, "text/plain", file.getName());
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We use &lt;code&gt;DefaultStreamedContent&lt;/code&gt; to set in the process data that then will be used by the &lt;code&gt;p:fileDownload&lt;/code&gt;. Note that the &lt;code&gt;FileInputStream&lt;/code&gt; is not closed. Doing that would automatically abort the download. To avoid memory leaks, the &lt;code&gt;DefaultStreamedContent&lt;/code&gt; will call the &lt;code&gt;close&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;For more information, please see the &lt;a href="http://xpert-ivy-hacker-source-code.googlecode.com/svn/trunk/Forms"&gt;example project&lt;/a&gt; or have a look at the &lt;a href="http://www.primefaces.org/showcase/ui/fileDownload.jsf"&gt;PrimeFaces FileDownload demo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;br&gt;
&lt;br&gt;
Note: This question and answer was &lt;a href="http://blog.xpertivyhacker.ch/2013/03/xpertivy-code-rezept-file-download-mit-p-filedownload.html"&gt;originally posted&lt;/a&gt; by Heinrich Spreiter on his &lt;a href="http://blog.xpertivyhacker.ch/"&gt;Xpert.ivy Hacker blog&lt;/a&gt;. Henry, many thanks to you for your enthusiastic work. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">SupportIvyTeam</dc:creator><pubDate>Wed, 09 Oct 2013 12:39:38 -0400</pubDate><guid>https://answers.axonivy.com/questions/25/provide-file-download-in-a-html-user-dialog/26</guid></item></channel></rss>