Отобразить загруженный документ в средстве просмотра документов в виде простых символов - PullRequest
1 голос
/ 15 октября 2019

Я работаю над требованием, когда мне нужно загрузить файл, используя компонент 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>

до тех пор, пока операция загрузки не завершится успешно, средство просмотра документовкомпонент должен быть отключен или не виден. после операции загрузки содержимое должно отображаться в средстве просмотра документов с использованием слушателя. не могли бы вы помочь достичь этого

1 Ответ

1 голос
/ 15 октября 2019

Как и предлагал @Selaron. Я добавил логическое свойство, которое передается в отображаемый атрибут. Это будет выполнено, только если загрузка документа прошла успешно.

Ниже приведен фрагмент кода для справки.

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>

Бобовый класс

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

}
...