2016-06-15 9 views
1

SharePoint 2013でrest apiを使用して添付ファイルをアップロードしています。このためには、同期でアップロード添付ファイルのメソッドを呼び出す必要があります。angularjsのPromiseチェイン

アップロードの添付ファイルのメソッドを非同期と呼びますと、409の競合エラーが発生します。

どのようにチェーン約束は

ループのために約束のチェーン化の最良の方法で私を助けてください..私はそうで第二の付着最初の添付ファイルの成功のメソッドとを呼び出したいloop.i.eためでオブジェクト。添付ファイルを保存するための

コモン方式:

var saveFileAngularJS = function (file, url) { 
      var deferred = $q.defer(); 
      getFileBuffer(file).then(function (fileArrBuffer) { 
       $http({ 
        method: 'POST', 
        url: baseUrl + url, 
        headers: { 
         'Accept': 'application/json;odata=verbose', 
         'Content-Type': undefined, 
         'X-RequestDigest': jQuery("#__REQUESTDIGEST").val() 
        }, 
        data: new Uint8Array(fileArrBuffer), 
        transformRequest: []     
       }).then(function successCallback(data) { 
        deferred.resolve(data); 
        alert('Successfully saved.', data); 
       }, function errorCallback(error) { 
        deferred.reject(error); 
        alert('Failed to save!!!.', error); 
       }); 
      }); 
      return deferred.promise; 
     }; 

メソッドの呼び出し:

for (var i = 0; i < $scope.files.length; i++) { 
     var file = $scope.files[i]._file; 
     var response = lssDealService.insertAttachment(transactionId, file); 
} 

var insertAttachment = function (dealId, file) { 
      var attachmentUrl = listEndPoint + "/GetByTitle('TransactionList')/GetItemById(" + dealId + ")/AttachmentFiles/add(FileName='" + file.name + "')"; 
      return baseService.saveFile(file, attachmentUrl); 
     }; 

挿入アタッチメントはSAVEFILEメソッドを呼び出します。

私はループのためにこれを順番に実行したい、ループが完了したら、私はすべての約束を処理し、成功メッセージをユーザに表示する必要がある。

効果的な方法で連鎖約束を書くのを手伝ってください。

upload(0).then(handleResult_0).upload(1).then(handleResult_1).... 

とその下に示したように、ここで起こってArray.reduceを使用していただきました!

+0

「http://jsfiddle.net/jsengel/0ryvkvph/」を参照してください。 – nikhil

答えて

0

function uploadMyAttachements() { 
    return myAttachements.reduce(function(promise, attachment) { 
     return promise.then(function() { 
      return upload(attachment); 
     }) 
     .then(function(result) { 
      console.log('RESULT FOR LAST UPLOAD', result); 
     }); 
    }, Promise.resolve()); 
} 

function upload(attachment) { 
    //upload the attachment to sharepoint 
    //and return a promise here 
} 

uploadMyAttachements().catch(function(err) { 
    //if anything in the promise chain fails 
    //it stops then and there and CATCHED here 
}); 

さて、あなたは配列としての添付を考えてみましょう、私たちは約束のチェーンを作成します

$scope.attachments = []; //modified via binding. 

function uploadAttachments(){ 
//Reduce the files array into a promise array with the uploadOne method 
//then return the promise when every promise has been resolved or one has rejected. 
return $q.all($scope.attachments.reduce(uploadOne, [])); 
} 

function uploadOne(file){ 
//Upload one, return promise. Use $http or $resource. 
} 

//Note - a more advanced way of doing this would be to send the files as batch (one 
//$http post) as FormData. There are some good wrappers for angular. 

$scope.upload = function(){ 
uploadAttachments().then(function(results){ 
    //Array of results 
}).catch(function(e){ 
    //Error handler 
}); 
} 
:あなたは私の2ペニーを投げる
0

を期待通りに一つずつ実行
関連する問題