0

I am using PrimeFaces and I want to show the preview of uploaded image <p:graphicImage> immetately after the upload operation is performed.

Matteo
  • 14,696
  • 9
  • 68
  • 106
Arunprasad
  • 567
  • 4
  • 14
  • 29

1 Answers1

5

You can use Primefaces Dynamic Image Streaming

Like this

<p:fileUpload update="myImage" fileUploadListener="#{myBean.handleFileUpload}" ....

<p:graphicImage id="myImage" value="#{myBean.chart}" />


private StreamedContent chart;  

File chartFile = new File("someFile");

chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

I took some code snippet from the showcase... But its enough the get the general Idea...

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • You said : "I want to show the preview of uploaded image" , You don't have to save it to DB... just keep in your bean for the preview... I don't think you can preview something that you haven't uploaded yet... Unless its from web... and than you can simple use the url in your `p:graphicImage ` – Daniel Nov 30 '12 at 12:28
  • ya here problem is flow .jvm comes streamedContent method after checks the @PostConstruct method So ,nullpointer exception is occur ... – Arunprasad Nov 30 '12 at 13:43
  • @ Daniel ya your right i didn't save the image in DB .then i can't use url also .bcz i want to preview the image before going to save. – Arunprasad Nov 30 '12 at 13:46
  • 2
    you dont have to save to db in order to preview.. keep it in your bean and use `new DefaultStreamedContent...` as a source of your `p:graphicImage ` make your bean a view scoped atleast... – Daniel Nov 30 '12 at 13:48
  • My code is 'StreamedContent fileStream; getter&setter... public void save()throws IOException{ ... fileStream = new DefaultStreamedContent(new FileInputStream(getFile()),"image/jpeg"); ...} UI ' – Arunprasad Nov 30 '12 at 14:28
  • Above code shows exception can't find the property filestream – Arunprasad Nov 30 '12 at 14:30
  • `Controller.fileStream` ? with capital C letter ??? anyway it looks like some basic JSF issue.... is it managed bean with session /view annotation at all ? – Daniel Nov 30 '12 at 15:19