2

I want to upload large files to SharePoint Online but I am unable to use chunked upload of sharepoint REST API. I would like to see a working example.

The api is described in here using the methods

startUpload(GUID, stream1) continueUpload(GUID, 10 MB, stream2)
continueUpload(GUID, 20 MB, stream3)
finishUpload(GUID, 30 MB, stream4)

I found the same question in https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5596f87a-3155-4e4f-a6e8-8d38fa5e580d/office-365-onedrive-for-businesssharepoint-rest-api-startupload-command?forum=appsforsharepoint

but the solution uses C# and I need REST.

1 Answer 1

4

The following C# sample shows how to upload large file using the StartUpload, ContinueUpload, and FinishUpload REST endpoints:

using (var client = new WebClient())
{
    client.BaseAddress = webUri;
    client.Credentials = GetCredentials(webUri, userName, password);
    client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
    client.Headers.Add("X-RequestDigest", RequestFormDigest());

    var fileUrl = "/Documents/SharePoint User Guide.docx";
    var endpointUrlS = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/savebinarystream", webUri, fileUrl);
    var fileContent = System.IO.File.ReadAllBytes(fileName);

    var firstChunk = true;
    var uploadId = Guid.NewGuid();
    var offset = 0L;
    const int chunkSize = 2048; //<-set chunk size (bytes)
    using (var inputStream = System.IO.File.OpenRead(fileName))
    {
         var buffer = new byte[chunkSize];
         int bytesRead;
         while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
         {
              if (firstChunk)
              {
                  var endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/startupload(uploadId=guid'{2}')", webUri, fileUrl,uploadId);
                  client.UploadData(endpointUrl, buffer);
                  firstChunk = false;                           
              }
              else if (inputStream.Position == inputStream.Length)
              {
                  var endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/finishupload(uploadId=guid'{2}',fileOffset={3})", webUri, fileUrl, uploadId,offset);
                  var finalBuffer = new byte[bytesRead];
                  Array.Copy(buffer , finalBuffer , finalBuffer.Length);
                  client.UploadData(endpointUrl, finalBuffer);
              }
              else
              {
                  var endpointUrl = string.Format("{0}/_api/web/getfilebyserverrelativeurl('{1}')/continueupload(uploadId=guid'{2}',fileOffset={3})", webUri, fileUrl, uploadId, offset);
                  client.UploadData(endpointUrl, buffer);
              }
              offset += bytesRead;
              Console.WriteLine("%{0:P} completed", (((float)offset / (float)inputStream.Length) ));
          }
      }
 }
  • For consuming SharePoint REST Interface WebClient class is utilized, in particular UploadData Method for uploading data.
  • GetCredentials.cs - method for getting SharePoint Online credentials
  • SharePoint REST POST requests requires form digest to be specified, RequestFormDigest is intended for that purpose (you could find the implementation of it here)

Result

enter image description here

3
  • 1
    I've tried this and I get the following error: /personal/tenant_awbrazier_onmicrosoft_com/Documents/largefile.zip does not exist. when it goes to do the first chunk. If I upload a file with the same name it then works
    – Webfort
    May 24, 2016 at 11:23
  • 1
    I am looking same solution for REST API because I am using SharePoint online
    – Negi Rox
    Feb 12, 2018 at 17:13
  • I am also getting that error when uploading the first chunk Feb 7 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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