始终在数组中上传第一个文件 我循环遍历图像文件列表,通过http发送它们作为上传
for(var j=0;j<images.length;j++){
(function(idx,images){
console.log("idx"+JSON.stringify($scope.Files)+"====="+JSON.stringify(images));
var data = {"image":images[idx]};
var payload = new FormData();
for(var key in data){
payload.append(key, data[key]);
}
$http({
url: uploadUrl,
method: 'POST',
data: payload,
//assign content-type as undefined, the browser
//will assign the correct boundary for us
headers: {'token':token,'Access-Control-Allow-Origin': '*','Content-Type': undefined},
//prevents serializing payload. don't do it.
transformRequest: angular.identity
}).then(fucntion(response){
console.log("response :" + JSON.stringify(response));
});
})(j,images);
}
答案 0 :(得分:0)
顺序处理异步操作的另一种方法可能是使用递归函数来减少图像数组,如果你喜欢做不同的事情。
function upload(images) {
var image = images[0]; //the fist item in the array of images
var data = {
image: image
};
var payload = new FormData();
payload.append('image', data);
$http({
url: uploadUrl,
method: 'POST',
data: payload
}).then(function (response) {
console.log(response);
images.shift(); //Edit: Fixed this bit
if (images.length) {
upload(images); //call the upload function again
}
else {
console.log('all images uploaded'); //When the arrays length is 0 all images have been uploaded
}
});
}
upload(myArrayOfImagesInHere); //Initially call the function here with an array of images or things you want uploaded.
答案 1 :(得分:0)
为了让您顺序请求,您可以使用承诺。
aws s3 cp exe_file.exe s3://cdn/exe_file.exe --content-type "application/x-msdownload" --content-disposition "attachment; filename=\"exe_file.exe\"; filename*=utf-8''exe_file.exe" --metadata-directive REPLACE
答案 2 :(得分:0)
您可以使用$q.all
作为执行多个异步请求的简便方法。
var promises = [];
for(var j=0;j<images.length;j++){
(function(idx,images){
console.log("idx"+JSON.stringify($scope.Files)+"====="+JSON.stringify(images));
var data = {"image":images[idx]};
var payload = new FormData();
for(var key in data){
payload.append(key, data[key]);
}
var promise = $http({
url: uploadUrl,
method: 'POST',
data: payload,
//assign content-type as undefined, the browser
//will assign the correct boundary for us
headers: {'token':token,'Access-Control-Allow-Origin': '*','Content-Type': undefined},
//prevents serializing payload. don't do it.
transformRequest: angular.identity
});
promises.push(promise)
})(j,images);
}
$q.all(promises)
.then(function(responses) {
// responses available here
})