newbie
newbie

Reputation: 107

FileDownload resolves into null

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?

  1. Why is it renamed to null?
  2. How can i set the correct file extension in an automatical manner?

Upvotes: 1

Views: 1339

Answers (1)

dognose
dognose

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

Related Questions