Primefaces 文件下载不起作用? [英] Primefaces File Download not working?
问题描述
试图让一个简单的文件下载工作,但我得到的只是一个悬挂的 AJAX 状态栏,仅此而已.我的支持 bean 输出在准备和下载中呈现正确的名称.
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.0Primefaces 3.4
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());
}
推荐答案
Primefaces 版本指南 >= 10:
对于 10 之前的 Primefaces 版本,您必须使用 ajax=false
在 commandButton
上禁用 ajax.
For Primefaces versions before 10 you had to disable ajax on the commandButton
with ajax=false
.
自从版本 10 不再需要,请参阅 Primefaces 文档 10.
Since version 10 that's no longer needed, see Primefaces Documentation 10.
Primefaces 版本指南10:
如果您想使用 PrimeFaces commandButton 和 commandLink,请禁用 ajax 选项作为fileDownload 需要刷新整个页面才能显示文件.
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>
这篇关于Primefaces 文件下载不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!