【问题标题】:JSF Primefaces Upload/Process/Download all on one page that enables Download after UploadJSF Primefaces 上传/处理/下载全部在一个页面上,允许上传后下载
【发布时间】: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”的组件,并且页面根本不会呈现
  • 那是因为你有它在另一个表单中。将您的另一个命名为&lt;h:form id="formOfTheButton"&gt;,并在您的更新中使用update=":formOfTheButton:download"
  • 好的,让我添加它作为你的答案。
  • 用第二种解决方案查看我的答案

标签: html file jsf primefaces


【解决方案1】:

您需要更新命令按钮才能更改其状态。您可以通过将按钮移动到相同的表单并为其添加update 属性来完成此操作,或者您命名您的第二个表单并使用它。所以:

第一个解决方案:

<h:form>
    <p:fileUpload id="upload" fileUploadListener="#{fileProcessor.process}" mode="advanced" update="download"/>

    <p:commandButton id="download" value="Download" ajax="false" disable="#{!(fileProcessor.readyToDownload)}">
        <p:fileDownload value="#{fileProcessor.zipfile}" />
    </p:commandButton>
</h:form>

第二种解决方案:

<h:form>
    <p:fileUpload id="upload" fileUploadListener="#{fileProcessor.process}" mode="advanced" update=":secondForm:download"/>
</h:form>

<h:form id="secondForm">
    <p:commandButton id="download" value="Download" ajax="false" disable="#{!(fileProcessor.readyToDownload)}">
        <p:fileDownload value="#{fileProcessor.zipfile}" />
    </p:commandButton>
</h:form>

【讨论】:

  • 完美!谢谢!
猜你喜欢
  • 2013-08-25
  • 1970-01-01
  • 2012-06-11
  • 2013-01-30
  • 1970-01-01
  • 2013-05-11
  • 2019-01-23
  • 2017-03-17
  • 2013-11-19
相关资源
最近更新 更多