Search code examples
primefacesprimefaces-extensions

Display uploaded document in document viewer in primefaces


I am working on requirement where i need to upload a file using fileupload component in primefaces and display the same file using documentViewer component on the same page.

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

until upload operation completes successfully, document viewer component should be disabled or not visible. after upload operation, the content should be displayed in document viewer using listener. can you please help to achieve this


Solution

  • As suggested @Selaron. I added boolean property which passed to rendered attribute. It would be made true only if uploading the document is success.

    Below is code snippet for reference.

    HTML CONTENT

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