0

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 message in chrome

enter image description here

and is the stack trace I get when I try preview pdf using chrome browser

24-Feb-2021 20:29:57.256 SEVERE [http-nio-8084-exec-255] com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError java.io.FileNotFoundException: demo_receipt.pdf (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at co.sf.controller.UploadController.load_stream_Content(UploadController.java:129)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)

and this is my managed bean code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.sf.controller;

//imports

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

  

    public UploadController() {
    }
    
    StreamedContent streamedContent;

    public void load_stream_Content() throws Exception {
        Path fileName = Paths.get(image.getSubmittedFileName());
       String path= fileName.toString();
  
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
          
            streamedContent = new DefaultStreamedContent();
        } else {
          String fs = Paths.get(image.getSubmittedFileName()).getFileName().toString(); 
          

         
            streamedContent=new DefaultStreamedContent(new FileInputStream(new File(path)), "application/pdf", fs);//error is occuring here
          
        }
        streamed = true;
    }

    boolean streamed = false;

    public boolean isStreamed() {
        return streamed;
    }

and here is my jsf page

<?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>
     
    </h:head>
    <h:body>
        <h:form enctype="multipart/form-data">
            <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>
             <h:commandButton value="Upload" action="#{userData.doUpload()}">
                           <f:ajax  execute="@form" render="@form"/>
             </h:commandButton>
        </h:form>
        
        
               <p:media id="pdf_preview" rendered="#{userData.streamed}"  width="100%" height="700px" value="#{userData.streamedContent}"
            cache="false" player="pdf"/>
      
        
    </h:body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Morgan Denis
  • 111
  • 1
  • 15
  • @JasperdeVries kindly check the error has changed and I rectivied the code as suggested in the duplicate code and did not work – Morgan Denis Feb 25 '21 at 03:11
  • @JasperdeVries how does the duplicate questions resolves my question?? – Morgan Denis Feb 25 '21 at 03:14
  • @BalusC am a newbie in jsf and cant relate what you recommended with my post – Morgan Denis Feb 25 '21 at 10:44
  • @BalusC My uploads works fine so my concern is I don't want to save the file first without preview and that why I posted this question. If you are kind enough you can help me resolve it will really appreciate – Morgan Denis Feb 25 '21 at 10:57
  • @BalusC I need immediately a user click on browse button he should see a preview of what he has selected before hitting upload button – Morgan Denis Feb 25 '21 at 10:58
  • @BalusC what do u mean by bytes[] would you kindly show me. am completely stuck on this from yesterday – Morgan Denis Feb 25 '21 at 10:59

0 Answers0