试图让一个简单的文件下载工作,但我得到的只是一个悬挂的 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());
}
Primefaces 版本指南 >= 12.0.0
在 PrimeFaces 11 之前,您必须通过 DataExport 禁用 AJAX。从版本 11 开始,不再需要它。
Primefaces 版本指南 >= 10 && < 12.0.0
对于 10 之前的 Primefaces 版本,您必须使用
commandButton
禁用 ajax=false
上的 ajax。
自版本 10 起不再需要,请参阅 Primefaces 文档 10。
Primefaces 版本指南< 10:
如果您想使用 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>
在命令按钮内,设置 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;
}
从 PrimeFaces 10 及更高版本开始,您可以使用 Ajax 下载!
https://primefaces.github.io/primefaces/10_0_0/#/components/filedownload?id=ajax-downloading
当您在命令按钮或链接上使用 Ajax 时,JavaScript 将触发下载。
另请参阅:
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>
爪哇语:
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;
}
}