17

试图让一个简单的文件下载工作,我得到的只是一个悬挂的 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>

支持豆:

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());
}
4

4 回答 4

31

Primefaces 版本指南 >= 10:

对于 10 之前的 Primefaces 版本,您必须在commandButtonwith上禁用 ajax ajax=false

由于不再需要版本 10,请参阅Primefaces 文档 10

Primefaces 版本 < 10 的指南:

请参阅Primefaces 文档 6.2

如果您想使用 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>
于 2013-04-18T21:40:05.027 回答
15

在命令按钮内部,设置 ajax=false 并且不要对命令链接使用动作或动作侦听器。

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

豆:

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;
}
于 2014-07-23T08:53:19.557 回答
2

从 PrimeFaces 10 起,您可以使用 Ajax 下载!

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

当您在命令按钮或链接上使用 Ajax 时,下载将由 JavaScript 触发。

也可以看看:

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

于 2020-09-04T11:34:24.560 回答
0

ia 所做的最重要的事情是 turn ajax="false"。

这里我有commandlink,你可以在下面看到。

在 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>

在 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;
    }
}
于 2020-02-12T17:40:47.990 回答