我有一个表单可以生成一些记录,如下图 - > 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());
}
请提前给我你的建议和谢意。
答案 0 :(得分:0)
我在代码中看到您没有将特定行的实例路径传递给prepDownload()
方法,因此您需要修改数据表,如传递路径值到prepDownload(dt.path)
方法。我在这里假设dt是数据表变量,路径是变量,它存储在数据表中getList()
<p:dataTable var="dt" value="#{managedBean.getList()}">
.
//put your rest code...
.
<p:column>
<f:facet name="header">
<h:outputText value="Download" />
</f:facet>
<p:commandButton value="Download" ajax="false" actionListener="#{sBean.prepDownload(dt.path)}">
<p:fileDownload value="#{sBean.download}"/>
</p:commandButton>
</p:column>
</p:dataTable>
您需要在bean类中将prepDownload() method
更改为prepDownload(String instancepath)
,如:
public void prepDownload(String instancepath) throws Exception {
File file = new File(instancepath); //here passing instance path
.
.
//rest of code need to put same...
.
.
}
我试过这对我有用。