2012-05-05 19 views
3

我想實現一個ImageUpload並立即在同一頁上使用DynamicImage顯示上傳的圖像。我的問題是,我無法強制刷新p:graphicImage內容並在上傳後顯示上傳的圖像。顯示UploadedFile內容從Primeface的p:fileUpload以相同的形式沒有刷新

@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}" /> 

回答

10

如果您上傳做的工作

所有你需要做的是設置id來<p:graphicImage這樣

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

並在您的<p:fileUpload中設置更新屬性以指向圖片

這樣

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

謝謝你,這工作。 – nicmon

+0

不客氣。 – Daniel

+0

@丹尼爾我不能讓它工作。這是我的graphicImage 我使用servlet「DisplayImage 「而不是支持bean。因爲圖像不是存儲在數據庫中,而是存儲在文件系統上。所以我不使用StreamedContent,而是使用標準的輸入和輸出流 – rxlky