javavaadinmultifile-uploader

MultiFileUpload in Vaadin - I want to do some task when the last file is uploaded


I want to do two task at a time

  1. uploading multiple files.
  2. when all selected files are uploaded then I want to open a window and selecting their type and some other work and then saved it.

When I did it for single upload it works fine.

Now I'm trying to implement multiupload with MultiFileUpload addon But the event public void handleFile(InputStream in, String filename, String mimetype, long length) is fired for each of the files one by one. I want to open the window when the last file is uploaded. How can I detect that the last file was uploaded?

Here is my code:

protected void init(VaadinRequest vaadinRequest) {
    FormLayout layout = new FormLayout();
    UploadFinishedHandler uploadFinishedHandler = new UploadFinishedHandler() {
        @Override
        public void handleFile(InputStream in, String filename, String mimetype, long length) {
            try {
                FileOutputStream fos = null;
                String filePath = "/home/yassir/Desktop/";
                File dirFile = new File(filePath);
                if (!dirFile.exists()) {
                        dirFile.mkdirs();
                }
                filePath += filename;

                Button btn = new Button(filename);
                layout.addComponent(btn);
                File file = new File(filePath);
                fos = new FileOutputStream(file);
                byte[] buffer = new byte[in.available()];
                in.read(buffer);
                fos.write(buffer);
                fos.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MyUI.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MyUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

    UploadStateWindow uploadStateWindow = new UploadStateWindow();
    MultiFileUpload multiFileUpload = new MultiFileUpload(uploadFinishedHandler, uploadStateWindow);
    layout.addComponent(multiFileUpload);
    setContent(layout);
}

I will call the function createWindowType()(which i didn't add here).


Solution

  • You can handle the last file upload with AllUploadFinishedHandler.

    Something like this should work:

    multiFileUpload.setAllUploadFinishedHandler(new AllUploadFinishedHandler() {
        @Override
        public void finished() {
            //open window here
        }
    });
    

    This listener is going to be called every time when: