kartik-v / yii2-widget-fileinput

An enhanced FileInput widget for Bootstrap 4.x/3.x with file preview, multiple selection, and more features (sub repo split from yii2-widgets)
Other
230 stars 96 forks source link

Plugin events not fired #5

Closed gael-wogenstahl closed 9 years ago

gael-wogenstahl commented 9 years ago

I'm using the Yii2-FileInput as a no-preview, AJAX single file upload.

To be able to change my view on AJAX file upload complete I was counting on using the different plugin events mentioned in the documentation such as fileuploaded, filebatchuploadsuccess or filebatchuploadcomplete.

However those events aren't fired in my case. I've been debugging my application and found out that:

Can you help me with this situation ? I can definitely see when my files are uploaded since the FileInput change back to an empty input and my file is received on server side but I have no way to interact on client side and update my view (this is actually to display the uploaded file name and prevent the user from uploading another file).

kartik-v commented 9 years ago

First, on the events not visible in source, it seems you are having an outdated plugin version. Cross check with source and update.

Second, you may read the docs and the events section for the various events:

fileuploaded fires for asynchronous batch-file upload - (parallel server upload - and this event is triggered after every single file in the batch is uploaded) - OR this event is also fired for a single file upload.

filebatchuploadsuccess fires for synchronous batch-file upload... (basically one server call for a batch of files) - and fires ONCE after the server completes the batch upload...

filebatchuploadcomplete fires in both cases (sync & async).

gael-wogenstahl commented 9 years ago

Indeed there was something wrong with the source. I cleaned and freshly install the yii2-widgets extension again and now I can see where the events are fired.

There is still a little issue/enhancement though with the filebatchuploadsuccess event. It's fired when my file is uploaded successfully however the outData object passed with the event does contain reference to the current filestack (which is empty after file upload) and not the finished one (which contains the file descriptor of the file successfully uploaded). It seems the setAllUploaded(); does clear the filestack and thus the outData object filestack reference.

Would it feel right for you to fire the event BEFORE clearing the filestack and setting the different widget elements ? A PR could be this (in uploadBatch) :

...
if(typeof data.error === 'undefined' || isEmpty(data.error)) {
    // Moved from line 1021 to 1009
    self.$element.trigger('filebatchuploadsuccess', [outData]);
    setAllUploaded();
    if (self.showPreview) {
        self.$preview.find('.kv-file-upload').hide();
        self.$preview.find('.kv-file-remove').hide();
        self.$preview.find('.file-preview-frame').each(function() {
            var $thumb = $(this), key = $thumb.attr('data-fileindex');
            setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
            enableActions(key);
        });
    } else {
        self.reset();
    }
} else {
...

In any case your advise to be able to get the name of the file successfully uploaded through plugin events will be greatly appreciated.

kartik-v commented 9 years ago

The plugin is upgraded to release v4.1.4 which includes various enhancements including the above.