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