Home AngularJS Foreach not waiting
Reply: 0

AngularJS Foreach not waiting

user1661
1#
user1661 Published in April 21, 2018, 3:25 pm

I have am using angularjs trying to upload multiple files to my sharepoint API so what I did I use foreach statement with $q.all to wait for everything to finish uploading but somehow my code is already considered resolved on the first upload of file.

factory.UploadFilesByBatch = function(arrayBuffer, folderName) { var promises = [];

    angular.forEach(arrayBuffer, function(item) {
        // factory.GetFormDigest().then(function(data) {
        var deferred = $q.defer();
        // promises.push(deferred);

        console.log("UploadFilesByBatch Try - " + item.fileName + "Uploading")
        $http({
            url: webUrl + "web/getfolderbyserverrelativeurl('" + folderLocation + "')/files/add(overwrite=true, url='" + item.fileName + "')",
            method: "POST",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": xrequestDigest,
                "content-Type": "application/json;odata=verbose"
            },
            data: item.fileBuffer,
            transformRequest: []
        }).then(function(result) {
            console.log("UploadFilesByBatch Success - " + item.fileName + "Uploaded")
            item.sharepointFileLocation = result.data.d.ServerRelativeUrl;
            deferred.resolve(item);

        }, function(result, status) {
            console.log("UploadFilesByBatch Error - " + item.fileName + "Failed")
            console.log(JSON.stringify(result))
            deferred.reject(result);
        });
        promises.push(deferred);

        // })

    })
    return $q.all(promises);

};
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.366197 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO