horacioj
horacioj

Reputation: 737

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" ?

Upvotes: 2

Views: 3509

Answers (2)

Mahesh Nakka
Mahesh Nakka

Reputation: 27

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

Upvotes: 3

Michael Mainer
Michael Mainer

Reputation: 3465

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.

Upvotes: 0

Related Questions