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

前端 未结 2 1956
清酒与你
清酒与你 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:12

    New and improved large file upload for the .NET client library

    With the fluent client

    Create upload session

    // Create upload session 
    // POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession
    var uploadSession = await graphClient.Drive.Items[itemId].ItemWithPath("SWEBOK.pdf").CreateUploadSession().Request().PostAsync();
    

    Create the task

    // Create task
    var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
    var largeFileUpload = new LargeFileUpload(uploadSession, graphClient, stream, maxChunkSize);
    

    Create upload monitor

    public class MyProgress : IProgressCallback
    {
        public void OnFailure(ClientException clientException)
        {
            Console.WriteLine(clientException.Message);
        }
    
        public void OnSuccess(DriveItem result)
        {
            Console.WriteLine("Download completed with id below");
            Console.WriteLine(result.Id);
        }
    
        public void UpdateProgress(long current, long max)
        {
            long percentage = (current * 100) / max ;
            Console.WriteLine("Upload in progress. " + current + " bytes of " + max + "bytes. " + percentage + " percent complete");
        }
    }
    

    Upload the file

    uploadedFile = await largeFileUpload.ResumeAsync(new MyProgress());
    

    With the HTTP client

    Create upload session

    // Create upload session 
    // POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession
    string uri = $"https://graph.microsoft.com/v1.0/drive/items/{itemId}:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession";
    
    HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
    await graphClient.AuthenticationProvider.AuthenticateRequestAsync(httpRequestMessage);
    
    // Read the session info from the response
    var httpResponseMessage = await graphClient.HttpProvider.SendAsync(httpRequestMessage);
    var content = await httpResponseMessage.Content.ReadAsStringAsync();
    var uploadSession = graphClient.HttpProvider.Serializer.DeserializeObject(content);
    

    Create the task

    // Create task
    var maxSliceSize = 320 * 1024; // 320 KB - Change this to your chunk size. 4MB is the default.
    LargeFileUploadTask largeFileUploadTask = new LargeFileUploadTask(uploadSession, stream, maxSliceSize);
    

    Create upload monitor

    // Setup the progress monitoring
    IProgress progress = new Progress(progress =>
    {
        Console.WriteLine($"Uploaded {progress} bytes of {stream.Length} bytes");
    });
    

    Upload the file

    UploadResult uploadResult = null;
    try
    {
        uploadResult = await largeFileUploadTask.UploadAsync(progress);
    
        if (uploadResult.UploadSucceeded)
        {
            Console.WriteLine($"File Uploaded {uploadResult.ItemResponse.Id}");//Sucessful Upload
        }
    }
    catch (ServiceException e)
    {
        Console.WriteLine(e.Message);
    }
    

提交回复
热议问题