0

I want to download user selected files with primefaces. I was able to do so for a specific file as described in the primface showcase for "file Download". But what I actually want is, that after pressing the "download Button" a file dialog should open, so the user can select a file for himself to download. Is that possible?

My current code for a specific file download lokks like this:

    public void handleLanguageFileDownload() throws FileNotFoundException, IOException {

        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        File fileToDownload = new File(DataManager.configreader.getLang_source());
        String fileName = fileToDownload.getName();
        String contentType = ec.getMimeType(fileName);
        int contentLength = (int) fileToDownload.length();

        ec.responseReset(); 
        ec.setResponseContentType(contentType); 
        ec.setResponseContentLength(contentLength); 
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 

        OutputStream output = ec.getResponseOutputStream();

        Files.copy(fileToDownload.toPath(), output);

        fc.responseComplete(); 
}

I want the exact same behaviour for file upload, so the user can select the folder to upload files to for himself. My current implementation uploads the file only to a specific folder.

My current code for uploading files to a specific folder looks like this:

    public void handleLanguageFileUpload(FileUploadEvent event) throws IOException {

        if (!this.role.canManageLanguage){
            return;
        }

        String [] filePathParts =     DataManager.configreader.getLang_source().split("/");
        String uploadPathString = DataManager.configreader.getLang_source().replaceAll(filePathParts[filePathParts.length - 1],""); //event.getFile().getFileName()
        File uploadPath = new File(uploadPathString);
        File fileToUpload = new File(uploadPath, event.getFile().getFileName());

        try (InputStream input = event.getFile().getInputstream()) {
            if(event.getFile().getFileName().equals(filePathParts[filePathParts.length - 1])) { //fileToUpload.getName()
            Files.copy(input, fileToUpload.toPath(), StandardCopyOption.REPLACE_EXISTING);
            uiManager.userMessage.info (event.getFile().getFileName() + " " + this.translate("msg_has_been_uploaded") + " !");
            this.getOwnTreeVersion();
            }
            else {
                uiManager.userMessage.error (event.getFile().getFileName() + " " + this.translate("msg_is_wrong_cannot_be_uploaded") +": " + filePathParts[filePathParts.length - 1] + " !");
            }
        }
    }

Thank you in advance for your help!!!

1 Answers1

-1

working with file chooser is only possible with the Upload method look at this post to understand how to implement it in your project Upload File Step by Step and you can even read more in the Primefaces web site Primefaces Upload File if you need to add a FileSizeLimite and many other features.

Now for the download method i told you that it's impossible because you have a default file location (generally it's Download) you can read more about it in the Primefaces web site Primefaces Download File you can set it manually but it will not be dynamic look at this post Change Download Path.

Community
  • 1
  • 1
Yagami Light
  • 1,756
  • 4
  • 19
  • 39