Primefaces文件下载不起作用?

时间:2013-04-18 21:37:07

标签: 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>

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

4 个答案:

答案 0 :(得分:25)

请参阅Primefaces Documentation 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>

答案 1 :(得分:12)

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

<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;
}

答案 2 :(得分:2)

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

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

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

另请参阅:

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

答案 3 :(得分:0)

ia所做的最重要的事情是把ajax =“ false”变成。

在这里,我可以使用命令链接,如下所示。

在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;
    }
}
相关问题