I have a SharePoint 2013 on premise provider hosted App and i am trying to upload large files greater than 10MB(Ex: 40 MB) through SharePoint cross domain Rest API calls using Sp.RequestExecutor. 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** error on browsers (IE,Chrome etc.,). I read from an article(MSDN
Blog), To upload the file to SharePoint this must be converted into a string which can then be set as the body of a POST operation. This can be done by iterating through the array and building up a string using the string.fromCharCode() function.
I have converted my file to string array and passing it in the Rest call body as suggested in the article but i am only able to upload files less than 40MB but not more than that. When ever i try to upload a large file the conversion to string array takes a
bit of time and then the Rest call gets initiated. Once the call gets initiated i get an error **(Out of Memory)** and the file is not uploaded. I have tried increasing the maxRequestLength="51200" executionTimeout="999999" in my provider-hosted
app web.config file but still no difference.
Could someone please help me with uploading large files through SharePoint 2013 on premise Rest API? Below is my code, 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 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;
};