I am trying to implement ImageUpload and show the loaded image immediately on the same page using DynamicImage. My problem is that I cannot get the contents of p: graphicImage to be updated and show the downloaded image after it has been downloaded.
@ManagedBean(name = "myformbean")
@Controller
@ViewScoped
@Data
public class MyFormBean implements Serializable {
private StreamedContent listImage = null;
public StreamedContent getListImage() {
if (listImage == null) {
try {
listImage = new DefaultStreamedContent(new FileInputStream("E:/t.jpg"), "image/png");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return listImage;
}
public void handleFileUpload(FileUploadEvent event) {
final UploadedFile uploadedFile = event.getFile();
listImage = new DefaultStreamedContent(new ByteArrayInputStream(uploadedFile.getContents()), "image/png");
}
}
And in the .xhtml file:
<p:graphicImage value="#{myformbean.listImage}" />
source
share