JSF Primefaces在一个页面上传/处理/下载所有内容,启用上传后下载

时间:2018-04-02 17:59:59

标签: html file jsf primefaces

我想要一个带有Uploader的页面来处理上传的文件,创建一个新文件,并使该文件可用于下载。我可以全部工作,除了我想要禁用Download commandButton,直到处理发生并且文件可供下载。这是不起作用的部分。 “下载”按钮始终处于启用状态。

<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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要更新命令按钮才能更改其状态。您可以通过移动相同表单的按钮并为其添加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>