我正在使用angularjs尝试将多个文件上传到我的sharepoint API,所以我使用foreach语句和$ q.all等待所有内容完成上传,但不知怎的,我的代码已经被认为已经解决了第一次上传文件。
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);
};
答案 0 :(得分:0)
You're not quite using the $q.all correctly. You want to push the actual promise into your array of promises. You'll also want to get rid of all of the deferred stuff, the $http call already is a promise so you can push that directly into your promise array.
Instead of (?<=[a-z]\\.\\s)
You want to do
promises.push(deferred);
This will leave you with an array of promises. You can then use promises.push($http({
url: webUrl + "web/getfolderbyserverrelativeurl('" + folderLocation + "')/files/add(overwrite=true, url='" + item.fileName + "')",
method: "POST",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": data.d.GetContextWebInformation.FormDigestValue,
"content-Type": "application/json;odata=verbose"
},
data: item.fileBuffer,
transformRequest: []
})
);
to iterate over the results like so:
$q.all(promises)