I have form with many input fields plus primefaces component to upload multiple file "p:fileUpload" when I submit the form I can't get the uploaded files .. the manged bean is "RequestScoped" . So how can I get the uploaded files without making the manged bean View scope?
the upload 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);
}
}
primefaces 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 the 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);
}
So how can I get the uploaded files without making the manged bean View scope?
Just store the upload information immediately in a more permanent place than as a property of a request scoped bean which get garbaged by end of request-response anyway (note: every upload counts 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 some key to access them, consider storing the key in the session scope.