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.

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
user3129040
  • 167
  • 1
  • 3
  • 12

3 Answers3

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>
Rahmat Anjirabi
  • 868
  • 13
  • 16
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();
            });
        }
Dirty-flow
  • 2,306
  • 11
  • 30
  • 49
  • 3
    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. – Vitani Jan 15 '14 at 16:25
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

necrox87
  • 21
  • 3
  • 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. – Mir Sep 23 '15 at 10:05