【问题标题】:Using chunked upload/StartUpload with sharepoint REST api将分块上传/StartUpload 与 sharepoint REST api 一起使用
【发布时间】:2016-02-09 21:47:37
【问题描述】:

我想在线上传大文件到 sahrepoint,但我无法使用 sharepoint REST api 的分块上传。我想看看一个可行的例子。

这里描述了 api https://msdn.microsoft.com/EN-US/library/office/dn450841.aspx#bk_FileStartUpload 使用方法

开始上传(GUID,stream1) 继续上传(GUID,10 MB,stream2)
继续上传(GUID,20 MB,stream3)
完成上传(GUID,30 MB,stream4)

我在 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

但该解决方案使用 C#,我需要 REST。

【问题讨论】:

    标签: sharepoint sharepoint-online chunked


    【解决方案1】:

    以下 C# 示例展示了如何使用 StartUploadContinueUploadFinishUpload REST 端点覆盖现有文件:

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

    用法

    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);
    
    • 使用 SharePoint REST 接口 WebClient class,特别是 UploadData Method 用于上传数据。
    • GetCredentials.cs - 获取 SharePoint Online 凭据的方法
    • SharePoint REST POST 请求需要表单摘要,RequestFormDigest 用于此目的(您可以找到 实现它here)

    结果

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多