Click here to Skip to main content
15,884,388 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
   
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
        <style>
            *{
                margin: 0px;
                padding:0px;
            }
            #pdf_div{
                height: 100%;
                width: 100%;
            }
         

            .ui-widget-content{
                margin-top: 0px;
            }
         

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

        
    </h:body>
</html>


and here is my managed bean
Java
//imports

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

  

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

    public void load_stream_Content() throws Exception {
        inputStream=image.getInputStream();
      //  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.
Posted

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