2013-02-18 69 views
0

我目前工作的JSF PrimeFaces,我想下載文件,該文件存在於我的項目名稱爲「CV」文件夾,但這裏面臨的問題是低於JSF Primefaces下載文件

文件上傳代碼:

<h:form enctype="multipart/form-data"> 
    <p:fileUpload 
     fileUploadListener="#{fileUploadController.handleFileUploadCv}" 
     mode="advanced" update="messages" 
     allowTypes="/(\.|\/)(doc|docx)$/" /> 
    <p:growl id="messages" showDetail="true" /> 
</h:form> 

文件下載:

<h:form id="form11"> 
    <p:commandButton id="downloadLink" value="Download Cv" ajax="false" 
     onclick="PrimeFaces.monitorDownload(start, stop)" 
     icon="ui-icon-arrowthichk-s"> 
     <p:fileDownload value="#{fileDownloadController.file}" /> 
    </p:commandButton> 
</h:form> 

這裏是控制器類:

import java.io.InputStream; 
import org.primefaces.model.DefaultStreamedContent; 
import org.primefaces.model.StreamedContent; 
@ManagedBean 
@SessionScoped 
public class FileDownloadController { 

    private StreamedContent file; 

    public FileDownloadController() { 

     InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("D:/Final Year Project/displayjob-portlet/docroot/cv/Junaid.cv"); 
     System.out.print("inside download111"); 
     file = new DefaultStreamedContent(stream); 
    } 

    public StreamedContent getFile() { 
     return file; 
    } 
} 
+0

你是「面臨的問題」。告訴我們,這些是什麼。 – Zhedar 2013-02-18 17:04:43

+0

coud'nt能夠下載file.no迴應 – 2013-02-18 17:05:38

+1

請檢查您的瀏覽器的服務器日誌和錯誤控制檯,然後說出你在那裏得到什麼。如果您在日誌中發現錯誤,第二個對您沒有任何興趣。 – Zhedar 2013-02-18 17:06:30

回答

1

您正試圖從您的電腦獲取文件,而不是從服務器獲取文件。由於應用程序在服務器上運行,因此根目錄是您的服務器域根目錄。 嘗試將文件放入服務器並更改其目錄。 例如,您可以在服務器的根目錄中創建一個名爲filesToDownload的文件夾,並將其放入其中。然後,你會發現它是這樣的:

InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("filesToDownload/Junaid.cv"); 

我知道這個問題是有點老了,但我面臨同樣的問題,我想這個答案可能會幫助別人。

0

您是否在頁面中添加了該腳本;

<script type="text/javascript"> 
function start() { 
    PF('statusDialog').show(); 
} 

function stop() { 
    PF('statusDialog').hide(); 
} 
</script>