【发布时间】:2015-12-31 06:14:04
【问题描述】:
我有一个生成一些记录的表单,如下图--> prime faces datatable image。
每一行都有一个下载按钮,它会生成一个唯一的文件。 我想从下载按钮中的每一行数据中下载一个文件。
当我点击第一行的下载按钮时,下载了一个文件。接下来,我单击了第二行下载按钮,但它再次下载了第一行文件。
我为每一行的下载按钮获取相同的文件。
我的代码:
xhtml 页面:
<p:column>
<f:facet name="header">
<h:outputText value="Download" />
</f:facet>
<p:commandButton value="Download" ajax="false" actionListener="# {sBean.prepDownload}">
<p:fileDownload value="#{sBean.download}"/>
</p:commandButton>
</p:column>
Bean 类:
StudentReportPojo info = null;
if (list != null && list.size() > 0) {
for(StudentReportPojo aa:list){
filepath=aa.getPath();
System.out.println("file path of file "+filepath);
//prepDownload(filepath);
}
}
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(filepath);
FileInputStream 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());
}
请提前给我您的建议和感谢。
【问题讨论】:
-
您应该花点时间正确地格式化您的代码。它使我们更容易阅读
-
我在
prepDownloadactionListener 或getDownload中看到 nothing 相信您有任何逻辑可以在每一行中下载不同的文件。提示:将 something 传递给 action 或 actionListener 或<p:fileDownload>的值属性,以便可以下载另一个文件。例如。将filepath传递给 ActionListener,如#{sBean.prepDownload(datatableVarAttribute)}
标签: jsf primefaces