Primefaces文件下载不起作用?

use*_*871 16 jsf primefaces

试图让一个简单的文件下载工作,我得到的是一个悬挂的AJAX状态栏,就是这样.我的支持bean输出在准备和下载时呈现正确的名称.

我做错了吗?在我看来这两个输出都是正确的.

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>
Run Code Online (Sandbox Code Playgroud)

支持豆:

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());
}
Run Code Online (Sandbox Code Playgroud)

Man*_*uel 28

请参阅Primefaces文档6.1

如果您想使用PrimeFaces commandButton和commandLink,请禁用ajax选项,因为fileDownload需要整页刷新才能显示该文件.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false" actionListener="#{filemanagement.prepDownload}">
    <p:fileDownload value="#{filemanagement.download}" />
  </p:commandButton>
</h:form>
Run Code Online (Sandbox Code Playgroud)


小智 13

在命令按钮内部,设置ajax = false,不要对commandlink使用动作或动作侦听器.

<h:form>
  <p:commandButton id="downloadLink" value="Download" ajax="false">
    <p:fileDownload value="#{filemanagement.downloadValue}" />
  </p:commandButton>
</h:form>
Run Code Online (Sandbox Code Playgroud)

豆:

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;
}
Run Code Online (Sandbox Code Playgroud)