【问题标题】:Display uploaded document in document viewer in primefaces在 primefaces 的文档查看器中显示上传的文档
【发布时间】:2019-10-15 09:46:14
【问题描述】:

我正在处理需要使用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>

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

【问题讨论】:

  • 您不需要p:tabView 来实现这一点。将rendered="#{not empty basicDocumentViewerController.content}" 添加到您的pe:documentViewer。或者甚至更好地向您的 bean 添加一个布尔属性,如果存在文档,则返回 true 并在 rendered 属性中询问。
  • 我暗示您的解决方案是可行的,只要您没有说明“直到上传操作成功完成,文档查看器组件应该被禁用或不可见”旁边的特定问题。

标签: primefaces primefaces-extensions


【解决方案1】:

正如@Selaron 所建议的那样。我添加了传递给渲染属性的布尔属性。只有上传文件成功才会生效。

下面是代码sn-p供参考。

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>

Bean 类

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2015-05-18
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多