Below is the code I use in my project to allow user to upload file(s). For each file selected , "$scope.upload" function gets invoked asynchronously.
Because these are asynchronous calls , each call does not depend on previous call.
But my requirement is only after success call back function gets executed, then the next upload should happen,because each upload depends on values returned by previous upload.
Please let me know how it can be achieved. Note : $scope.upload is part angular file upload js (https://github.com/danialfarid/ng-file-upload)
$scope.startUploading = function ($files, errFiles, item) {
//$files: an array of files selected
for (var idx = 0; idx < $files.length; idx++) {
var $file = $files[idx];
(function (index) {
$scope.upload[index] = $upload.upload({
url: "../UploadCommentAttachment",
method: "POST",
file: $file,
data: item
}).success(function (responseData, status, headers, config) {
item.AuditCommentAttachments.push(responseData);
item.CommentId = responseData.CommentId;
item.AuditId = responseData.AuditId;
item.Operation = "EDIT";
item.CommentAttachmentID = responseData.CommentAttachmentID;
}
});
})(idx);
} ///-- for loop end
} // startuploading end