Whenever I attempt to upload multiple attachments to my list, I encounter a 'save conflict' error. It seems that Sharepoint is still processing the previous attachment when a new one is submitted.
I believe that introducing a delay before sending the next file could potentially resolve this issue. However, there is concern that if the file being uploaded is large, the delay may be too short for it to be processed effectively.
Currently, my method of uploading attachments involves using promises with $q
.
var elementPromises = [];
angular.forEach(element.files, function(item){
item = $scope.UploadAttachment(item).then(function(){});
elementPromises.push(item);
});
$q.all(elementPromises).then(function () {
// alert('all attachments saved');
}, function(reason) {
// alert('Failed: ' + reason);
}, function(update) {
//alert('Got notification: ' + update);
});
The UploadAttachment function looks like this:
$scope.UploadAttachment = function(file){
var deferred = $q.defer();
setTimeout(function() {
// deferred.notify('Saving attachments..');
readFile(file).done(function (buffer, fileName) {
var saveFile = new Entry(buffer);
saveFile.$upload({ID: ID, filename: fileName}, function(u){
console.log(u);
deferred.resolve('Success');
}, function(error){
console.log(error);
deferred.reject('Error');
});
});
}, 1000);
return deferred.promise;
};
The ReadFile function utilizes filereader to retrieve a buffer and filename, which are then uploaded to SharePoint.
The 'Entry' in 'New Entry (buffer)' is a factory of type '$resource' and contains the following '$upload' function:
upload: {
url: "serverURL/_api/lists/getByTitle('listName')/items(:ID)/AttachmentFiles/add(FileName=':filename')",
method: "POST",
transformRequest: [],
processData: true,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": function() {return $("#__REQUESTDIGEST").val()},
"content-length": function () {
return arguments[0].data.byteLength;
}
}
}
While this approach works efficiently for single files or small attachments, encountering the save conflict error with multiple larger files has been challenging.
My current solution involves implementing a delay, but I am uncertain about how to properly integrate it, as well as whether it will completely resolve the issue. Any insights on this matter would be greatly appreciated.