Reputation: 299
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();
}
}
Upvotes: 0
Views: 1706
Reputation: 21
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.
Upvotes: 2