2017-06-22 100 views
0

我想根據通過primeup中的fileuploader上傳的文件顯示x個消息/ growl。爲PrimeFaces顯示fileupload的多個消息/咆哮消息

目前,它只會顯示最新的消息。其餘的將不會顯示。無論如何,最後的節目可以繞過嗎?或另一種選擇?

所以,如果我上傳4個文件。成功與否,我希望它顯示4條消息。 1爲每個文件。

的index.xhtml:

<h:form id="form"> 
     <p:messages id="messages" autoUpdate="true" showDetail="true" closable="true" /> 

     <p:fileUpload fileUploadListener="#{fileUploadAction.upload}" 
      mode="advanced" allowTypes="/(\.|\/)(asc)$/" dragDropSupport="true" update="messages" /> 
</h:form> 

FileUploadAction.java:

public void upload(FileUploadEvent event) 
    { 
     String filename = event.getFile().getFileName(); 

     try 
     { 
      copyFile(filename, event.getFile().getInputstream()); 

      FacesContext.getCurrentInstance().addMessage(filename, new FacesMessage(FacesMessage.SEVERITY_INFO, "msg.header", filename + " is uploaded.")); 
     } 
     catch (IOException e) 
     { 
      FacesContext.getCurrentInstance().addMessage(filename, new FacesMessage(FacesMessage.SEVERITY_ERROR, "err.header", e.getLocalizedMessage())); 
     } 
    } 

    public void copyFile(String fileName, InputStream in) 
    { 
     try 
     { 
      // write the inputStream to a FileOutputStream 
      OutputStream out = new FileOutputStream(new File(PropertiesUtil.LOCAL_PATH + fileName)); 

      int read = 0; 
      byte[] bytes = new byte[1024]; 

      while ((read = in.read(bytes)) != -1) 
      { 
       out.write(bytes, 0, read); 
      } 

      in.close(); 
      out.flush(); 
      out.close(); 
     } 
     catch (IOException e) 
     { 
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "err.header", e.getLocalizedMessage())); 
     } 
    } 
+0

你的意思表示具有比一個消息的更多,當您上傳您的文件?!? –

+0

@YagamiLight是的。就像我上傳4個文件和4個文件成功。我將顯示4條消息。 1爲每個文件。 – shadow

+0

首先,如果您想製作多個上傳文件,請在閱讀完[Primefaces Upload Multiple File Example](https://www.primefaces.org/showcase/ui/file/upload/multiple.xhtml)後告訴我如果問題仍然存在 –

回答

1

由於文件上傳正在更新消息或每個文件上傳咆哮,前一消息被從DOM清除特別是如果請求得到快速服務。

爲了保持咆哮完好,並顯示多個消息可能會執行以下操作:

首先咆哮組件:

<p:growl widgetVar="growlWV" showDetail="true" /> 

確保您定義的widgetVar,以後將使用它。

文件上傳:

<p:fileUpload fileUploadListener="#{fileUploadAction.upload}" 
     mode="advanced" allowTypes="/(\.|\/)(asc)$/" dragDropSupport="true" /> 

確保你沒有更新的文件上傳的咆哮。

二在上傳處理方法:

RequestContext.getCurrentInstance().execute("PF('growlWV').renderMessage(" 
      + "{\"summary\":\"summary goes here\"" 
      + ", \"detail\":\"" + event.getFile().getFileName() + "\"" 
      + ", \"severity\":\"warn\"})"); 

您可以用低凌亂的語法定義自己的js函數,更整潔然後調用上述的js代替所有這些+ \在執行,但我想你明白了。


相關問題: Show growl using Javascript