【发布时间】:2018-04-02 17:59:58
【问题描述】:
我想要一个带有 Uploader 的页面,它可以处理上传的文件、创建一个新文件并使该文件可供下载。我可以让这一切正常工作,除了我希望禁用下载命令按钮,直到处理完成并且文件可供下载。这是不起作用的部分。下载按钮始终处于启用状态。
<h: form>
<p: fileUpload id="upload" fileUploadListener="#{fileProcessor.process}" mode="advanced" />
</h: form>
<h: form>
<p:commandButton id="download" value="Download" ajax="false" disable="#{!(fileProcessor.readyToDownload)}">
<p:fileDownload value="#{fileProcessor.zipfile}" />
</p:commandButton>
</h: form>
这是支持 bean:
@ManagedBean
@ViewScoped
public class FileProcessor {
private InputStream zipfileInputStream = null;
private boolean readyToDownload = false;
public StreamedContent getZipfile(){
return new DefaultStreamedContent(zipfileInputStream, "application/zip", "processed.zip");
}
public boolean isReadyToDownload(){
return readyToDownload;
}
public void process(FileUploadEvent event){
InputStream uploadStream = event.getFile().getInputStream();
// a separate class processes the data, returns an InputStream
zipfileInputStream = FileProcessProvider.getInstance().process(uploadStream);
if(zipfileInputStream != null){
readyToDownload = true;
}
}
}
【问题讨论】:
-
你没有更新你的组件,使用
update属性重新渲染下载按钮 -
我试过了,但我得到了一个 FacesException:找不到从“j_idt28:upload”引用的表达式“download”的组件,并且页面根本不会呈现
-
那是因为你有它在另一个表单中。将您的另一个命名为
<h:form id="formOfTheButton">,并在您的更新中使用update=":formOfTheButton:download" -
好的,让我添加它作为你的答案。
-
用第二种解决方案查看我的答案
标签: html file jsf primefaces