Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When multiple upload is finished in p:fileUpload?

I have a multiple p:fileUpload, I need to upload from 1 to N files on server, after uploading I need to know how many files were uploaded and then start processing method. How can I know when all files are uploaded? I have not found any event for multiple upload, there is event only for individual files, oncomplete in p:fileUpload also works for every file individually. I had idea to use additional button to invoke processing method, but this is not safe, because user can push this button before all files are uploaded. Who can advice me something?

like image 747
user2993815 Avatar asked Dec 23 '13 16:12

user2993815


People also ask

How do I upload multiple files online?

Depending on the version of OneDrive or SharePoint that you're using, you may also be able to upload multiple files by holding down either the Ctrl or Shift key, and selecting more than one file.

What is Fileupload?

Uploading is the transmission of a file from one computer system to another, usually larger computer system. From a network user's point-of-view, to upload a file is to send it to another computer that is set up to receive it.


1 Answers

For other people who may face the same problem.

The file upload component has 'files' property which contain the list of files to be uploaded. As soon as a file uploaded it is removed from the 'files' array, thus when last file is uploaded the 'files' will be empty. This could be used to detect the moment when the last file is uploaded. Afterwards you can trigger remote command to do whatever you want.

Code example:

<h:form enctype="multipart/form-data">
    <p:fileUpload id="files"
             widgetVar="fileUploadWidget"
             mode="advanced"
             multiple="true"
             oncomplete="handleMultiFileUploadRequest(PF('fileUploadWidget'), validateFiles);"/>

    <p:remoteCommand name="validateFiles" actionListener="#{Your action}"/>

    <script type="text/javascript">
        function handleMultiFileUploadRequest(fileupload, remoteButton) {
            if (fileupload.files.length === 0) {
                if (remoteButton) {
                    remoteButton();
                }
            }
        }
    </script>
</h:form>
like image 186
Mikhail Chibel Avatar answered Sep 28 '22 01:09

Mikhail Chibel