2016-05-23 59 views
0

Java EE 7 - Primefaces 5.3
我正在上傳文件列表,然後返回另一個頁面,該頁面提供訪問用戶下載這些文件的權限。當用戶點擊下載按鈕時,我將fileName作爲屬性傳回給我的控制器,然後返回要下載的文件。如果文件可用,我正在尋找一種呈現下載按鈕的方法。那可能嗎?如果是這樣如何?如何使用primefaces提供文件的渲染文件的下載按鈕5.3

下面是文件的DataTable在download.xhtml:

<p:dataTable var="file" value="#{controller.fileList}"> 
    <p:column headerText="File Name"> 
     <p:outputLabel value="#{file.fileName}"/> 
    </p:column> 
    <p:column headerText="download"> 
     <p:commandButton value="Download" id="download" onclick="PrimeFaces.monitorDownload(start, stop);" ajax="false" actionListener="#{controller.prepareToDownload}" icon="ui-icon-arrowthick-1-s"> 
       <f:attribute name="fileName" value="#{file.fileName}" /> 
       <p:fileDownload value="#{controller.download}" /> 
     </p:commandButton> 
    </p:column> 

控制器:

public void prepareToDownload(ActionEvent actionEvent) throws FileNotFoundException { 
     String directory = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("uploadDirectory"); 
     fileName = (String)actionEvent.getComponent().getAttributes().get("fileName"); 
     file = new File(directory, fileName); 
       InputStream input = new FileInputStream(file); 
     ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); 
     download = new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName()); 
    } 

public void setDownload(DefaultStreamedContent download) { 
     this.download = download; 
    } 

    public DefaultStreamedContent getDownload() throws Exception { 
     return download; 
    } 
+1

渲染或disabled屬性通過使用呈現的屬性? – Kukeltje

回答

0

我看到兩個選項:

  1. controller.fileList()提供準備下載的文件=>無chan下載一個不存在的文件
  2. controller.fileList()CES提供的文件列表,包括文件尚未準備好下載=>使用的命令按鈕