在 primefaces 的文档查看器中显示上传的文档

Display uploaded document in document viewer in primefaces

我正在处理需要使用 primefaces 中的 fileupload 组件上传文件并在同一页面上使用 documentViewer 组件显示相同文件的要求。

<h:form  enctype="multipart/form-data">
        <p:fileUpload value="#{basicDocumentViewerController.file}"  mode="simple"></p:fileUpload>
                <p:separator/>
                <h:commandButton value="Upload file" action="#{basicDocumentViewerController.dummyAction}">
                </h:commandButton>
            <p:tabView>  
                <p:tab title="Display content of the file">  
                    <pe:documentViewer id="documentViewer"  height="500" value="#{basicDocumentViewerController.content}" />  
                </p:tab>  
            </p:tabView>  
        </h:form>

在上传操作成功完成之前,文档查看器组件应该被禁用或不可见。上传操作后,内容应使用侦听器显示在文档查看器中。你能帮忙实现这个吗

按照@Selaron 的建议。我添加了传递给渲染属性的布尔值 属性。只有上传文件成功才会生效

以下是供参考的代码片段。

HTML 内容

 <h:body>
        <h:form  enctype="multipart/form-data">
        <p:fileUpload value="#{basicDocumentViewerController.file}"  mode="simple"></p:fileUpload>
                <p:separator/>
                <h:commandButton value="Dummy Action" action="#{basicDocumentViewerController.dummyAction}">
                </h:commandButton>
                    <pe:documentViewer id="documentViewer" rendered="#{basicDocumentViewerController.contentAvailable}" height="500" value="#{basicDocumentViewerController.content}" download="extensions-rocks.pdf"/>  
        </h:form>
    </h:body>

豆子class

@ManagedBean(name = "basicDocumentViewerController")
@SessionScoped
public class BasicDocumentViewerController implements Serializable {

    private static final long serialVersionUID = 1L;

    private StreamedContent content;
    private UploadedFile file;
    private boolean contentAvailable =false;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public StreamedContent getContent() throws IOException {
        if(content == null){
            content=pdfDocumentGenerate();
        }
        return content;
    }

    public String dummyAction(){
        System.out.println("Uploaded File Name Is :: "+file.getFileName()+" :: Uploaded File Size :: "+file.getSize());
        setContentAvailable(true);
        return "";
    }

    public void setContent(StreamedContent content) {
        this.content = content;
    }


    public DefaultStreamedContent pdfDocumentGenerate() throws IOException {

        try {
            byte[] document = IOUtils.toByteArray(file.getInputstream());
            return new DefaultStreamedContent(new ByteArrayInputStream(document), "application/pdf", "Actor_List");

        }finally{

        }
    }

    public boolean isContentAvailable() {
        return contentAvailable;
    }

    public void setContentAvailable(boolean contentAvailable) {
        this.contentAvailable = contentAvailable;
    }

}