2

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;
        };
1

If you are developing for Sharepoint Online, you could use the new StartUpload/ContinueUpload/FinishUpload API. With this API, you can divide your Upload into chunks and upload every chunk separately.

If you are developing an on Premise Solution you could write a Mediator WebService, which performs the upload for you. The Mediator Webservice would be a simple Webservice with one method "Upload", which internally uses SaveBinaryDirect or the ContentStream from the FileInformationCreate Object to upload the file using the .net CSOM library. The Webservice must also handle the Authentication and implement CORS Headers. Mediator Webservice

If it is an option, you could also wait for Sharepoint 2016 where the new API will most likely be included.

  • Hi Harrison, thanks for your reply. I am using SharePoint 2013 on Premise solution. Could you please post more information on how to proceed through a Mediator WebService. My current solution is completely client side scripting and i cannot use any server code. Appreciate if you could tell any other approach for Sharepoint 2013 on premise way. – user545359 Mar 4 '16 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.