user2124871
user2124871

Reputation: 813

Primefaces File Download not working?

Trying to get a simple file download working and all I am getting is a hanging AJAX status bar and that's it. My backing bean outputs render the correct name on the prep and the download.

Am I doing this wrong? both outputs seem to me to be correct.

JSF 2.0 Primefaces 3.4

        <h:form>
            <p:commandButton id="downloadLink" value="Download" actionListener="#{filemanagement.prepDownload}"> 
                <p:fileDownload value="#{filemanagement.download}" />
            </p:commandButton>
        </h:form>

Backing bean:

private DefaultStreamedContent download;

public void setDownload(DefaultStreamedContent download) {
    this.download = download;
}

public DefaultStreamedContent getDownload() throws Exception {
    System.out.println("GET = " + download.getName());
    return download;
}

public void prepDownload() throws Exception {
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
}

Upvotes: 18

Views: 50946

Answers (4)

Manuel
Manuel

Reputation: 4228

Guide for Primefaces version >= 12.0.0

Before PrimeFaces 11, you had to disable AJAX with DataExport. As of version 11 that's no longer needed.

Primefaces Documentation 12.0.0

Guide for Primefaces version >= 10 && < 12.0.0

For Primefaces versions before 10 you had to disable ajax on the commandButton with ajax=false.

Since version 10 that's no longer needed, see Primefaces Documentation 10.

Guide for Primefaces version < 10:

See Primefaces Documentation 6.2

If you’d like to use PrimeFaces commandButton and commandLink, disable ajax option as fileDownload requires a full page refresh to present the file.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false" actionListener="#{filemanagement.prepDownload}">
    <p:fileDownload value="#{filemanagement.download}" />
  </p:commandButton>
</h:form>

Upvotes: 32

Jasper de Vries
Jasper de Vries

Reputation: 20168

From PrimeFaces 10 and up you can use Ajax downloads!

https://primefaces.github.io/primefaces/10_0_0/#/components/filedownload?id=ajax-downloading

The download will be triggered by JavaScript when you use Ajax on your command button or link.

See also:

https://github.com/primefaces/primefaces/issues/5978

Upvotes: 3

Lucas Campos
Lucas Campos

Reputation: 11

The most important thing that ia have done is turn ajax="false".

Here i have with commandlink, as you can see bellow.

In html:

<p:commandLink id="someId"
                value="Download"
                style="padding-left: 2em; vertical-align: middle;"
                ajax="false"
                onstart="PF('pageBlocker').show()"
                oncomplete="PF('pageBlocker').hide()">
  <p:fileDownload value="#{bean.downloadFileTemplate()}" />
</p:commandLink>

In Java:

public StreamedContent downloadFileTemplate() {
    try {
        FileInputStream inputStream = new FileInputStream(new File(getClass().getClassLoader().getResource("path_to_resource/myfile.xlsx").getFile()));

        StreamedContent fileTemplate = new DefaultStreamedContent(
                inputStream
                , "application/vnd.ms-excel"
                , "my_file.xlsx");

        return fileTemplate;
    } catch (Exception e) {
        getLog().error("Error on download...", e);
        return null;
    }
}

Upvotes: 0

Manbumihu Manavan
Manbumihu Manavan

Reputation: 702

Inside command button, set ajax=false and do not use action or action listener for commandlink.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false">
    <p:fileDownload value="#{filemanagement.downloadValue}" />
  </p:commandButton>
</h:form>

Bean:

public StreamedContent getDownloadValue() throws Exception {
    StreamedContent download=new DefaultStreamedContent();
    File file = new File("C:\\file.csv");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    download = new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()));
    System.out.println("PREP = " + download.getName());
    return download;
}

Upvotes: 17

Related Questions