Click here to Skip to main content
15,116,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to upload a pdf file in jsf but before upload I would wish to allow user to see preview of the pdf before hitting submit button but its failing and giving this error in console
org.primefaces.application.resource.StreamedContentHandler.handle Error in streaming dynamic resource. Stream Closed

here is my xhtml code

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
        <title>Facelet Title</title>
                margin: 0px;
                height: 100%;
                width: 100%;

                margin-top: 0px;

        <h2>Internalization Language Example</h2>
        <h:form enctype="multipart/form-data" id="submit_form">
            <p:messages closable="true"/>
            <div id="upload-button">
               <h:inputFile   value="#{userData.image}">
                    <f:passThroughAttribute name="accept" value="*.pdf"/>
                    <f:ajax event="change"  listener="#{userData.load_stream_Content}"  render=":pdf_preview"/>
            <p:commandButton ajax="false" update="submit_form" value="Upload" action="#{userData.doUpload()}">
        <p:outputPanel id="pdf_preview" >
             <p:media   width="100%" height="700px" value="#{userData.streamedContent}"
            cache="false" player="pdf"/>


and here is my managed bean

@ManagedBean(name = "userData", eager = true)
public class UploadController implements Serializable {


    public UploadController() {
    StreamedContent streamedContent;
	private InputStream inputStream;

    public void load_stream_Content() throws Exception {
      //  String fileName = image.getSubmittedFileName();
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            streamedContent = new DefaultStreamedContent();
        } else {
              streamed = true;
          String fs = Paths.get(image.getSubmittedFileName()).getFileName().toString(); 
            streamedContent=new DefaultStreamedContent(inputStream, "application/pdf", fs);
        streamed = true;


    public StreamedContent getStreamedContent() {
        return streamedContent;

    public void setStreamedContent(StreamedContent streamedContent) {
        this.streamedContent = streamedContent;

    //getter setter
    private Part image;
    private boolean upladed;
	    boolean streamed = false;


What I have tried:

I need to show a pdf preview before upload. I have tried the code in the question and gives the error shown.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900