How to upload a large document in c# using the Microsoft Graph API rest calls

前端 未结 2 1962
清酒与你
清酒与你 2021-01-20 16:42

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 anythi

2条回答
  •  一个人的身影
    2021-01-20 17:07

    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();
        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
            // ...
        }
    }
    

提交回复
热议问题