0

I have a simple upload listener inside a view scoped bean that, for each file uploaded, adds it to a list and than displays the list.
The problem is that, when I press the upload button to upload more than one file at once, only one of the files is added to the list and no exception is shown. On the other hand, if I do the upload of a single file, waiting the previous to complete, the behavior is normal.
I thought of some concurrency problem but then, when I tried to put the bean in session scope, it worked correctly. Is it possible that a concurrency problem invalidates the view?
Any other suggestion? Thanks a lot

<h:form id="form" enctype="multipart/form-data">

    <p:wizard widgetVar="wiz" render="true" id="wizard">

        <p:tab id="p0" title="file upload" step="0">
            <p:panel>
                <p:fileUpload
                    fileUploadListener="#{myBean.uploadedFile}"
                     mode="advanced" multiple="true" sizeLimit="100000" 
                     update="fileList"/>

                <p:dataList id="fileList" value="#{myBean.filesName}" var="file">#{file}</p:dataList>

The bean:

public void uploadedFile(FileUploadEvent event) { 

    try {
        files.add(event.getFile());

        filesName.add(event.getFile().getFileName());

    } catch (Exception e) {

        e.printStackTrace();
    }

}
Daniela Mogini
  • 299
  • 1
  • 5
  • 17
  • what if you add process="@this" and remove enctype? – Darka Jan 18 '13 at 16:32
  • @Darka `enctype="multipart/form-data"` is necessary when submitting a file upload through a web form (that's a HTML - general web development concept). – Luiggi Mendoza Jan 18 '13 at 19:25
  • I guess that you should use a `@SessionScoped` managed bean in order to make your example work. – Luiggi Mendoza Jan 18 '13 at 19:25
  • Can you confirm that the listener is being called multiple times(once for each file)? – kolossus Jan 18 '13 at 20:11
  • 1
    I have worked with `p:fileUpload` in `@ViewScoped` beans and can say that you can maintain the bean after the upload happens. Just be sure you follow this rules http://stackoverflow.com/a/8880083/1199132. Also take care if you're using other filtering libraries like prettyfaces, you can have conflicts between them and the upload filter http://stackoverflow.com/a/9086137/1199132 – Aritz Jan 19 '13 at 12:18
  • 1
    @Luiggi Mendoza As I wrote in the question, I already noticed that it works with a session scoped but thanks for the advice. Is it mandatory to use a session bean? – Daniela Mogini Jan 21 '13 at 08:22
  • @Xtreme Biker Thanks, yes I follow the rules. In view scoped it works only if you upload one file then you wait the end of the upload, upload the second file and so on... I wanted to know if this is a known issue of primefaces uploader and if is possible to solve it or I should mandatory use a session bean. – Daniela Mogini Jan 21 '13 at 08:24
  • @kolossus Yes, I can confirm that it's executed once for each file. – Daniela Mogini Jan 21 '13 at 08:25

1 Answers1

2

I've just been stuck in the same situation like yours. After debugging hard, I finally found a solution that can help you too.

I think this problem comes from ViewScoped scope, in managing session context. So I try to manage the session context by myself. Use this code to initialize the session and your list:

    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(true); 
    session.setAttribute("files", new ArrayList<UploadedFile>());

You must initialize the list, it's MANDATORY. Then in your handleFileUpload method, just use the attribute "files" saved in session context to keep your upload files. Now your method can be able to handle MULTIPLE upload files.

  • When storing into the session you'll have it shared across all views and in addition it will use up JVM memory unless session serialization is used. – Archimedes Trajano Sep 29 '17 at 16:49
  • What would be better is a temporary table or file store to keep the uploaded file and associate it with the view with a periodic cleanup process. – Archimedes Trajano Sep 29 '17 at 16:51