4

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);
    }
Mohamed Habib
  • 883
  • 1
  • 8
  • 18
  • Please post the relevant JSF and Java Bean code also you may want to tag it with java and Spring/JEE – dngfng Oct 16 '12 at 11:14
  • 1
    it a lot of work to get the jsf page and javaBean code here , I'm Sorry the problem is when I uploading files it send request to the bean and uploaded successfully but when I complete the form and submit the form the method can't see the uploaded files – Mohamed Habib Oct 16 '12 at 11:27
  • Not all of it - just the relevant parts, how are we going to be able to help you if we don't know what your code looks like! – dngfng Oct 16 '12 at 11:29
  • Why don't you just give the `input` field to the user and only one `upload` button to the user ? I mean, the user choose the files to upload first then upload all together. – Valter Silva Oct 16 '12 at 11:29
  • @dngfng : i updated the code. I wish it helps you to help me :) – Mohamed Habib Oct 16 '12 at 11:36
  • @ValterHenrique : I have form to create new talent and user can upload multiple files he want then when he submit the form it create talent with this files .. not need to make it in two steps ( create talent then go to page to upload the files which that is working now) – Mohamed Habib Oct 16 '12 at 11:39

1 Answers1

5

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Now I have two choices : 1- store files on disk , but how can i get them back 2- store files on DB , but my DB table need id of talent that i upload files for and this option make me requests the db as much as the count of files (bad performance) – Mohamed Habib Oct 16 '12 at 11:56
  • I don't recommend store your files in DB, store only the path to the file in your disk, required much less space in your DB. – Valter Silva Oct 16 '12 at 12:12
  • 1
    As said, *"If you need some key to access them, consider storing the key in the session scope"*. You can if necessary just use the currently logged-in user as key. As to performance, measuring is knowing. Storing files in DB or not is merely a portability/reusability/maintainability/semanticity matter, not a performance matter. It's in the end just the very same disk file system hardware anyway. – BalusC Oct 16 '12 at 12:12