Show UploadedFile content from Primeface p: fileUpload in the same form without updating

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"); // load a dummy image
            }
            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}" />
+3
source share
1 answer

if your download does the job

all you have to do is set id to <p:graphicImagelike this

<p:graphicImage id="refreshMe" value="#{myformbean.listImage}" />

and in <p:fileUploadset the update attribute to point to the image

like this

<p:fileUpload  auto="true"  ... update="refreshMe" ....
+10
source

All Articles