如何使用Microsoft Graph API Rest调用在c#中上载大型文档 [英] How to upload a large document in c# using the Microsoft Graph API rest calls

查看:80
本文介绍了如何使用Microsoft Graph API Rest调用在c#中上载大型文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用外部.Net Web App,并且想知道如何使用Microsoft Graph将大文件上传到文档库.我最多可以上传4mb,但是高于4mb的任何东西都将引发错误.

I am using an external .Net Web App and would like to know how to upload a large file to the document library using Microsoft Graph. I am able to upload up to 4mb but anything above it is throwing an error.

我知道有一个createUploadSession,但是不确定如何实现.任何帮助将不胜感激.

I know there is a createUploadSession however not sure how to implement it. Any help would greatly appreciate.

这是我成功上传最大4mb的操作:

This is what I am doing to upload up to 4mb successfully:

string requestUrl =
    "https://graph.microsoft.com/v1.0/drives/{mydriveid}/items/root:/" +
    fileName + ":/content";

HttpClient Hclient = new HttpClient();

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, requestUrl);

message.Content = new StreamContent(file.InputStream);

client.DefaultRequestHeaders
    .TryAddWithoutValidation("Content-Type",
        "application/json; odata=verbose; charset=utf-8");

HttpResponseMessage Hresponse = await client.SendAsync(message);

//if the response is 200 then read the response and retrive the GUID!
if (Hresponse.IsSuccessStatusCode)
{
    responseString = await
    Hresponse.Content.ReadAsStringAsync();
    JObject jDataRetrieved = JObject.Parse(responseString);
    strGuid = jDataRetrieved.SelectToken("eTag").ToString();

}

推荐答案

您可以使用客户端库来帮助您完成此操作.来自这项测试:

You can use the client library to help you do this. From this test:

System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
var buff = (byte[])converter.ConvertTo(Microsoft.Graph.Test.Properties.Resources.hamilton, typeof(byte[]));
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buff))
{
    // Get the provider. 
    // POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/_hamiltion.png:/microsoft.graph.createUploadSession
    // The CreateUploadSesssion action doesn't seem to support the options stated in the metadata.
    var uploadSession = await graphClient.Drive.Items["01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ"].ItemWithPath("_hamilton.png").CreateUploadSession().Request().PostAsync();

    var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
    var provider = new ChunkedUploadProvider(uploadSession, graphClient, ms, maxChunkSize);

    // Setup the chunk request necessities
    var chunkRequests = provider.GetUploadChunkRequests();
    var readBuffer = new byte[maxChunkSize];
    var trackedExceptions = new List<Exception>();
    DriveItem itemResult = null;

    //upload the chunks
    foreach (var request in chunkRequests)
    {
        // Do your updates here: update progress bar, etc.
        // ...
        // Send chunk request
        var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);

        if (result.UploadSucceeded)
        {
            itemResult = result.ItemResponse;
        }
    }

    // Check that upload succeeded
    if (itemResult == null)
    {
        // Retry the upload
        // ...
    }
}

这篇关于如何使用Microsoft Graph API Rest调用在c#中上载大型文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆