How to save downloaded files in a form before sending it using jsf-2 and primefaces-3.4

I have a form with many input fields plus a "primary" component for uploading several files "p: fileUpload", when I submit the form, I can not get the downloaded files .. manged bean - "RequestScoped". So, how can I load downloaded files without creating a marked bean view area?

loading method

public void upload(FileUploadEvent event) { try { FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded."); FacesContext.getCurrentInstance().addMessage(null, msg); // Do what you want with the file String thumbnail = getDestination() + event.getFile().getFileName(); int index = thumbnail.lastIndexOf('.'); SystemFile systemFile = new SystemFile(); systemFile.setAccount(getActor().getAccount()); systemFile.setName(event.getFile().getFileName()); systemFile.setPath(getTalentPath()); systemFile.setFileType(FileUtil.checkFileType(thumbnail.substring(index + 1))); if (systemFiles == null) { systemFiles = new ArrayList<>(); } systemFiles.add(systemFile); copyFile(event.getFile().getFileName(), event.getFile().getInputstream()); } catch (IOException ex) { SystemLogger.getLogger(getClass().getSimpleName()).error(null, ex); } } 

component

  <p:fileUpload label="#{TalentMessages.lbl_Select_File}" fileUploadListener="#{talentPropertyAction.upload}" mode="advanced" multiple="true" uploadLabel="#{TalentMessages.lbl_upload_File}" cancelLabel="#{TalentMessages.lbl_cancel_File}" sizeLimit="2000000" oncomplete="completeUploadFile(#{talentPropertyAction.talentId});" /> 

then save function

  @Setter @Getter private List<SystemFile> systemFiles; try { // save something else then save the files if (systemFiles != null) { System.out.println("Not Null" + systemFiles); for (SystemFile systemFile : systemFiles) { TalentPropertyFile talentPropertyFile = new TalentPropertyFile(); talentPropertyFile.setTalentProperty(talentProperty); talentPropertyFile.setFile(systemFile); getTalentService().save(getActor().getAccount(), talentPropertyFile); } } else { System.out.println("Null"); } } catch (InvalidParameter ex) { SystemLogger.getLogger(getClass().getName()).error(null, ex); } 
+4
source share
1 answer

So how can I load downloaded files without creating a manged bean View scope?

Just save the download information immediately in a more permanent place than as a property of the area with the bean request, which in any case receives garbage from the end of the request-response (note: each download is considered as a separate HTTP request).

 public void upload(FileUploadEvent event) { // Now, store on disk or in DB immediately. Do not assign to a property. } public void save() { // Later, during submitting the form, just access them from there. } 

If you need a key to access them, consider storing the key in the session area.

+5
source

Source: https://habr.com/ru/post/1440024/


All Articles