/// <summary> /// Upload a large file using callbacks /// </summary> /// <param name="graphClient">Client for upload</param> /// <param name="itemId">itemId for upload</param> /// <returns></returns> public static async Task UploadLargeFileWithCallBacks(GraphServiceClient graphClient, string itemId) { try { using Stream stream = GetFileStream(); // POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession var uploadSession = await graphClient.Drive.Items[itemId].ItemWithPath("SWEBOK.pdf").CreateUploadSession().Request().PostAsync(); Console.WriteLine("Upload Session Created"); var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default. var largeFileUpload = new LargeFileUpload(uploadSession, graphClient, stream, maxChunkSize); // Setup the chunk request necessities DriveItem uploadedFile = null; try { // Simulate an exception uploadedFile = await largeFileUpload.UploadAsync(new MyProgressKiller()); } catch (TaskCanceledException) { //try to refresh the upload info and resume the upload from where we left off. Console.WriteLine("Resuming Download"); uploadedFile = await largeFileUpload.ResumeAsync(new MyProgress()); } //Sucessful Upload } catch (ServiceException e) { Console.WriteLine(e.Message); } //Sucessful Upload }
public static async Task UploadLargeFileInSlices(GraphServiceClient graphClient, string itemId) { try { using Stream stream = GetFileStream(); // 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 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); // Setup the chunk request necessities var slicesRequests = largeFileUpload.GetUploadSlicesRequests(); var trackedExceptions = new List <Exception>(); DriveItem itemResult = null; //upload the chunks foreach (var request in slicesRequests) { // Send chunk request var result = await largeFileUpload.UploadSliceAsync(request, trackedExceptions); // Do your updates here: update progress bar, etc. Console.WriteLine($"File uploading in progress. {request.RangeEnd} of {stream.Length} bytes uploaded"); if (result.UploadSucceeded) { itemResult = result.ItemResponse; Console.WriteLine($"File uploading complete"); } } // Check that upload succeeded if (itemResult == null) { //Upload failed Console.WriteLine("Upload failed"); } } catch (ServiceException e) { Console.WriteLine(e.Message); } }