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

6
  • i've done something like that (give the user the opportunity to select a file) is that what you are looking for ?!? Commented Oct 24, 2016 at 7:17
  • yes that is exactly what I want to do! Would be very nice, if you could help me out! Thanks!!! Commented Oct 24, 2016 at 7:23
  • it's a very complexe solution and understand that stackoverflow isn't here to give you a solution i will try to explain you how to do it Commented Oct 24, 2016 at 7:24
  • Do you work with Spring ?!? Commented Oct 24, 2016 at 7:26
  • No I don't work with Spring. Commented Oct 24, 2016 at 7:35

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

did you see this ?!?
hey, yeah I just saw it and thank you very much for your response, but this is too much for what I need.
I need the webb app to just open a dialog similar to a JFileChooser, so I can choose files, choose files and save them to a directory of my choice, is there an easier way to do this?
Ok see this post and tell me is it what do you need stackoverflow.com/questions/39486157/…
like it is in primefaces upload file is it true
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.