David
David

Reputation: 1144

ng-file-upload sequential promises

With this directive: ng-file-upload

How can I upload sequentially (in order, queue) multiple files one by one? I'm thinking about chained promises, but I don't know how can I combine promises and directive.

This is an example to upload multiple files, but all at the same time and not in order.

This is my code:

for (var i = 0; i < files.length; i++) {
    Upload.upload({
        url: config.base+'/upload/',
        data: {
            file: files[i],
        }
    }).then(function (response) {
        vm.reloadImatges();
        vm.upload.progress=0;
        vm.upload.files--;
    }, function (resp) {

    }, function (evt) {
        vm.upload.progress = parseInt(100.0 * evt.loaded / evt.total);
    });                        
}

Upvotes: 2

Views: 774

Answers (1)

David
David

Reputation: 1144

I found a solution, maybe not the best, but it works http://jsfiddle.net/erLax2fm/2/

Code below:

var doSomething = function (index) {

    var defer = $q.defer();

    Upload.upload({
        url: url: config.base+'/upload/',
        data: {
            file: objects[index]
        }
    }).then(function (response) {
        objects[index].processed = true;
        if (objects[++index]) {
            defer.resolve(index);
        } else {
            defer.reject();
        }
    }, function (response) {


    }, function (evt) {
       vm.upload.progress = parseInt(100.0 * evt.loaded / evt.total);
    });

    defer.promise.then(doSomething);
};

doSomething(0);

Upvotes: 2

Related Questions