以相同的形式显示来自Primeface的p:fileUpload的UploadedFile内容,无需刷新

时间:2012-05-05 18:48:33

标签: jsf file-upload primefaces

我正在尝试使用DynamicImage在同一页面上实现ImageUpload并显示上传的Image。我的问题是,我无法强制刷新p:graphicImage内容并在上传后显示上传的图像。

@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");
    }
}

在.xhtml文件中:

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

1 个答案:

答案 0 :(得分:10)

如果您的上传工作

您需要做的就是将ID设置为<p:graphicImage,就像这样

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

并在<p:fileUpload中设置更新属性以指向图片

像这样

<p:fileUpload  auto="true"  ... update="refreshMe" ....