innostudio / fileuploader

Beautiful and powerful HTML file uploading tool. A jQuery, PHP and Node.js plugin that transforms the standard input into a revolutionary and fancy field on your page.
141 stars 25 forks source link

onFilesCheck asnychronous? #70

Closed ghost closed 3 years ago

ghost commented 3 years ago

I'm encountering the problem that I have to check the length (in seconds) of a video file before upload. Sadly this is an asynchronous task, thus waiting for a callback. As far as I understand, the onFilesCheck Callback only allows for returning a boolean value. Is there any build-in possibility to pass back a Promise or callback?

Example (does actually not work, because it does not return boolean):

onFilesCheck(files, options, listEl, parentEl, newInputEl, inputEl) {
        let promises = Array.from(files).map(file => {
            return new Promise((resolve, reject) => {
                const video = document.createElement('video');
                video.preload = "metadata";
                video.onloadedmetadata = function () {
                    URL.revokeObjectURL(video.src);
                    var duration = video.duration;
                    if (duration > 10) {
                        reject('too long');
                    } else {
                        resolve();
                    }
                }
                video.src = URL.createObjectURL(file);
            });
        });
        return Promise.all(promises);
    }
onesideat commented 3 years ago

@ahlvd in the current version it is not possible. The only way will be (if you are using ajax mode) to set start: false and in onSelect to do the check. If the duration is ok, just to call item.upload.send();

ghost commented 3 years ago

Nice workaround. Thanks.