Junaid Akhtar
Junaid Akhtar

Reputation: 9

JSF Primefaces download file

I am currently working on JSF PrimeFaces, I want to download file which is present in my project name "CV " folder but facing issues here is the code below

File upload:

<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>

File download:

<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>

Here is the controller class:

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;
    }
}

Upvotes: 0

Views: 11371

Answers (2)

cristianorbs
cristianorbs

Reputation: 720

You are trying to get a file from your pc and not from the server. Since the application is running on a server, the root directory is your server domain root directory. Try to put the file inside the server and change the directory of it. For example you can create a folder inside the root directory of server called filesToDownload and put your file inside it. Then you should find it like this:

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

I know that this issue is a bit old, but as I was facing same problem, I think this answer might help someone else.

Upvotes: 1

Gokhan Ates
Gokhan Ates

Reputation: 1

Did you add that script in your page ;

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

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

Upvotes: 0

Related Questions