在primefaces中的文档查看器中显示上载的文档
我的工作要求,我需要上传一个文件使用primefaces中的fileupload组件,并显示相同的文件使用documentViewer组件在同一页上在primefaces中的文档查看器中显示上载的文档,primefaces,primefaces-extensions,Primefaces,Primefaces Extensions,我的工作要求,我需要上传一个文件使用primefaces中的fileupload组件,并显示相同的文件使用documentViewer组件在同一页上 <h:form enctype="multipart/form-data"> <p:fileUpload value="#{basicDocumentViewerController.file}" mode="simple"></p:fileUpload> &l
<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>
在上载操作成功完成之前,文档查看器组件应被禁用或不可见。上传操作完成后,应使用listener在文档查看器中显示内容。您能按照@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>
您不需要
p:tabView
来实现这一点。将rendered=“#{not empty basicDocumentViewerController.content}”
添加到pe:documentViewer
中。或者更好地向bean添加一个布尔属性,如果存在文档,则返回true,并在呈现的属性中询问该属性。我暗示您的解决方案是有效的,只要您没有在“在上载操作成功完成之前,文档查看器组件应被禁用或不可见”旁边说明特定问题
@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;
}
}