I am trying to upload large files greater than 10MB(Ex: 40 MB) through SharePoint cross domain Rest API calls. I am able to upload files that are upto 10MB using the code below. But when it comes to files that are greater than 10 MB i am getting an error saying Out of memory. Could someone please help me with uploading large files through SharePoint Rest API. Thanks in advance
Document upload Service call
$scope.docUpload = function () {
$(document).on("change", "#docfilebrowse", function () {
$scope.filearray = [];
$scope.filearray = $("#docfilebrowse")[0].files;
if ($("#docfilebrowse")[0].files.length > 0) {
for (var i = 0; i < $("#docfilebrowse")[0].files.length; i++) {
var content = $("#docfilebrowse")[0].files[i];
//Call 1: Upload document Rest call
$q.all([baseSvc.documentUpload('General Search Documents', '', content, 'DocumentUpload')]).then(function (data) {
//condition to check if the upload document is successful. The status code below will be sent back by the service after succcessful upload execution.
if (data[0].statusCode == "200") {
alert ('Success');
}
});
}
}
}
}
File Upload Code:
var documentUpload = function (librarytitle, foldername, filedata, requestType, success, failure) {
var deferred = $q.defer();
var reader = new FileReader();
reader.onload = function (result) {
var fileData = '';
var byteArray = new Uint8Array(result.target.result)
for (var i = 0; i < byteArray.byteLength; i++) {
fileData += String.fromCharCode(byteArray[i])
}
if (requestType == "DocumentUpload") {
var url = appweburl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/files/add(url=@TargetFileName,overwrite='true')?" +
"@TargetSite='" + hostweburl + "'" +
"&@TargetLibrary='" + librarytitle + "'" +
"&@TargetFileName='" + filename + "'";
}
// use the request executor (cross domain library) to perform the upload
var reqExecutor = new SP.RequestExecutor(appweburl);
reqExecutor.executeAsync({
url: url,
method: "POST",
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
contentType: "application/json;odata=verbose",
binaryStringRequestBody: true,
body: fileData,
success: function (data) {
//alert("Success! Your file was successfully uploaded to ClientPortal.");
var jObject = JSON.parse(data.body)
deferred.resolve(data);
},
error: function (x, y, z) {
console.log(z);
}
});
};
reader.readAsArrayBuffer(filedata);
return deferred.promise;
};