Start Upload all in jquery file upload blueimp
Asked Answered
M

3

12

I use jQuery file upload blueimp and have read

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        },
        add:function (e, data) {
            $("#uploadBtn").off('click').on('click',function () {
                data.submit();
            });
        }
    });
});

but this uploads a single file, I want to upload all files that have been selected.

Mcgill answered 23/12, 2013 at 9:21 Comment(3)
whats your form code mainly the input section have you set this to <input type="file" name="files[]" multiple> have you also looked at their API details?Fossa
I have read API but not understand, because english bad... pleaseMcgill
did you achievied how to solve your question?Ultra
L
8

your problem is that you unbind the click event on every file. you should do it on done:

done: function (e, data) {
            $("#uploadBtn").off('click')
            $.each(data.result, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        },
add: function (e, data) {
            $("#uploadBtn").on('click',function () {
                data.submit();
            });
        }
Landward answered 27/12, 2013 at 13:22 Comment(1)
The "point" here is that calling data.submit() on a single file will upload that one file, by calling data.submit() on all the files from one button (#uploadBtn) it will add them all to the queue and upload them in turn.Eldoneldora
I
10
var pendingList: [];

var sendAll: function () {
        pendingList.forEach(function (data) { data.submit(); });
        pendingList = [];
    };

$('#fileupload').fileupload({
    url: 'url_path',
    autoUpload: false,

    add: function (e, data) {
            pendingList.push(data);
        },


});

<button onclick="sendAll()">Start Upload All</button>
Indubitability answered 27/5, 2015 at 7:44 Comment(1)
That's the only way I can see how to do it. And it works.Alps
L
8

your problem is that you unbind the click event on every file. you should do it on done:

done: function (e, data) {
            $("#uploadBtn").off('click')
            $.each(data.result, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        },
add: function (e, data) {
            $("#uploadBtn").on('click',function () {
                data.submit();
            });
        }
Landward answered 27/12, 2013 at 13:22 Comment(1)
The "point" here is that calling data.submit() on a single file will upload that one file, by calling data.submit() on all the files from one button (#uploadBtn) it will add them all to the queue and upload them in turn.Eldoneldora
C
1

For upload concurrently all file you have selected, you can add the option autoUpload: true, like this:

$('#fileupload').fileupload({
    url: 'url_path',
    autoUpload: true
})

Reference: https://github.com/blueimp/jQuery-File-Upload/wiki/Options

Crista answered 23/9, 2014 at 10:56 Comment(1)
This answer is wrong, the autoUpload option triggers the upload when the file is selected. The question is about uploading more than one file at a button press.Tomi

© 2022 - 2024 — McMap. All rights reserved.