Using chunked upload/StartUpload with sharepoint REST api

你离开我真会死。 提交于 2019-12-08 11:39:36

问题


I want to upload large files to sahrepoint 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 https://msdn.microsoft.com/EN-US/library/office/dn450841.aspx#bk_FileStartUpload 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:


The following C# sample shows how overwrite an existing file by using the StartUpload, ContinueUpload, and FinishUpload REST endpoints:

    public static void UploadLargeFile(string webUrl, ICredentials credentials, string formDigest, string sourcePath, string targetFolderUrl,int chunkSizeBytes=2048)
    {
        using (var client = new WebClient())
        {
            client.BaseAddress = webUrl;
            client.Credentials = credentials;
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.Headers.Add("X-RequestDigest", formDigest);
            client.Headers.Add("content-type", "application/json;odata=verbose");

            //create an empty file first
            var fileName = System.IO.Path.GetFileName(sourcePath);
            var createFileRequestUrl = string.Format("{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files/add(url='{2}',overwrite=true)", webUrl, targetFolderUrl, fileName);
            client.UploadString(createFileRequestUrl, "POST");

            var targetUrl = System.IO.Path.Combine(targetFolderUrl, fileName);
            var firstChunk = true;
            var uploadId = Guid.NewGuid();
            var offset = 0L;

            using (var inputStream = System.IO.File.OpenRead(sourcePath))
            {
                var buffer = new byte[chunkSizeBytes];
                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}')", webUrl, targetUrl, 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})", webUrl, targetUrl, 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})", webUrl, targetUrl, uploadId, offset);
                        client.UploadData(endpointUrl, buffer);
                    }
                    offset += bytesRead;
                    Console.WriteLine("%{0:P} completed", (((float)offset / (float)inputStream.Length)));
                }
            }
        }

    }

Usage

var userCredentials = GetCredentials(userName, password);
var formDigest = RequestFormDigest(webUrl, userCredentials);
UploadLargeFile(webUrl,userCredentials,formDigest, @"C:\Users\jdoe\Documents\SampleVideo_1280x720_5mb.mp4", "/Shared Documents", 1024 * 1024);
  • 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, RequestFormDigest is intended for that purpose (you could find the implementation of it here)

Result



来源:https://stackoverflow.com/questions/33605892/using-chunked-upload-startupload-with-sharepoint-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!