简体   繁体   中英

Microsoft Graph - .NET SDK - OneDrive Large File Upload (> 4MB in size)

Using the .NET SDK for Microsoft Graph, you can upload (small) files. Example here .

How can I upload a large file (> 4MB) using the .NET SDK?

In other words, can the SDK be utilized to implement "Upload large files with an upload session" ?

Here is the code I wrote recently using Microsoft Graph .Net SDK. GraphServiceClient(graphClient) authentication is required.

    if (fileSize.MegaBytes > 4)
                    {
                        var session = await graphClient.Drive.Root.ItemWithPath(uploadPath).CreateUploadSession().Request().PostAsync();
                        var maxSizeChunk = 320 * 4 * 1024;
                        var provider = new ChunkedUploadProvider(session, graphClient, stream, maxSizeChunk);
                        var chunckRequests = provider.GetUploadChunkRequests();
                        var exceptions = new List<Exception>();
                        var readBuffer = new byte[maxSizeChunk];
                        DriveItem itemResult = null;
                        //upload the chunks
                        foreach (var request in chunckRequests)
                        {
                            // Do your updates here: update progress bar, etc.
                            // ...
                            // Send chunk request
                            var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);

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

                        // Check that upload succeeded
                        if (itemResult == null)
                        {
                            await UploadFilesToOneDrive(fileName, filePath, graphClient);
                        }
                    }
                    else
                    {
                        await graphClient.Drive.Root.ItemWithPath(uploadPath).Content.Request().PutAsync<DriveItem>(stream);
                    }

This will be available in the next release of .NET Microsoft Graph client library. It will work the same as the functionality in the .NET OneDrive client library. You can review this in my working branch . You can provide feedback in the repo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM