Reputation: 107
I have a filedownload that doesn't work as wanted. I followed the instructions provided in this thread, but the file that gets downloaded is just a file with the name null but of the size of the file i wanted to download originally. The command i use is
<p:commandLink ajax="false">
Download
<p:fileDownload value="#{dtEditView.file}"/>
</p:commandLink>
My bean consists of the following
public StreamedContent getFile() throws IOException {
path = "C:\\test.jpeg";
contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(path);
return new DefaultStreamedContent(new FileInputStream(path), contentType);
}
What am i doing wrong and what am i missing? I am using Primefaces 5.2.
Edit: I renamed the file into the file extension it is supposed to be and it appears to be what i wanted. The remaining questions are still?
Upvotes: 1
Views: 1339
Reputation: 20889
Just add the filename you want to use for download to the constructor of DefaultStreamedContent
:
return new DefaultStreamedContent(new FileInputStream(path), contentType, "filename.jpeg");
or to keep the same name as the resource:
return new DefaultStreamedContent(new FileInputStream(path), contentType, path.getFileName().toString());
compare with the bean in the showcase:
http://www.primefaces.org/showcase/ui/file/download.xhtml
Upvotes: 4