试图让一个简单的文件下载工作,我得到的只是一个悬挂的 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());
}
最佳答案
Primefaces 版本指南 >= 10:
对于 10 之前的 Primefaces 版本,您必须在 commandButton
上禁用 ajax。与 ajax=false
.
由于不再需要版本 10,请参阅 Primefaces Documentation 10 .
Primefaces 版本 < 10 的指南:
见 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>
关于jsf - Primefaces文件下载不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16093527/