2017-01-09 32 views
1

它總是上傳第一個文件在一個數組 餘米遍歷圖像文件的列表,用於發送它們通過HTTP上傳如何在一個文件列表中以循環的角度做同步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

所以你想要從圖像數組順序發送http請求(uploadUrl - POST)? – CodingFreak

+0

是的,幫我這個 –

+0

可能重複[AngularJS鏈承諾依次在for循環](http://stackoverflow.com/questions/41272992/angularjs-chain-promises-sequentially-within-for-loop) – CodingFreak

回答

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. 
+0

第一次形象是好的,之後它直接上傳最後一張圖片多次 –

+0

我的例子中有一個錯誤,圖片= images.shift()應該是'images.shif()'。已更新我的示例。 –

0

爲了讓您順序請求您可以使用承諾。

var promisesChain; 
var currentHttpPromise; 
var getHttpRequest=function(uploadUrl,payload,token){ 
    return $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)); 
      }); 
} 
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]); 
     } 

     if(idx==0){ 
      promisesChain=getHttpRequest(uploadUrl,payload,token); 
     } 
     else{ 
      (function(uploadUrl,payload,token){ 
       promisesChain.then(function(){ 
       return getHttpRequest(uploadUrl,payload,token) 
       }, 
       function(){ 
       //Error 
       }); 
      })(uploadUrl,payload,token); 

     } 
    })(j,images); 
} 
+0

仍然得到相同的結果 –

+0

有同樣的結果你的意思是http調用不是按順序的? –

+0

對於第一張圖片很好,之後直接上傳多張最後一張圖片 –

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 
}) 
+0

$ q.all不能保證所有的承諾都會順序執行 – CodingFreak

+0

啊,錯過了這個要求 – Fissio