I'm using LargeFileUploadTask from the Microsoft Graph SDK to upload large files. Occasionally (around 1 out of 10 uploads), the task gets stuck during the upload process. I stop receiving progress updates, and no exception is thrown. My internet connection is stable, so I don't believe the issue is network-related. I'm building a .NET MAUI Hybrid App and I'm testing on my Iphone 15 Pro.
Here’s my code:
public async Task<bool> UploadFileAsync(IProgress<long> percentProgress, string folder, string filename, Stream fileStream)
{
try
{
var uploadSessionRequestBody = new Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession.CreateUploadSessionPostRequestBody
{
Item = new DriveItemUploadableProperties
{
AdditionalData = new Dictionary<string, object>
{
{ "@microsoft.graph.conflictBehavior", "replace" },
},
},
};
var uploadSession = await _graphServiceClient.Drives[_driveId].Items["root"].ItemWithPath($"{_baseFolderPath}/{folder}/{filename}").CreateUploadSession.PostAsync(uploadSessionRequestBody);
fileStream.Position = 0;
var fileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, -1, _graphServiceClient.RequestAdapter);
var totalLength = fileStream.Length;
long percent = 0;
IProgress<long> progress = new Progress<long>(x =>
{
var actualPercent = x * 100 / totalLength;
if (actualPercent >= percent + 10)
{
percent = (actualPercent / 10) * 10;
percentProgress.Report(percent);
}
});
var uploadResult = await fileUploadTask.UploadAsync(progress);
return uploadResult.UploadSucceeded;
}
catch (ODataError ex)
{
Console.WriteLine($"Error uploading: {ex.Error?.Message}");
return false;
}
catch (Exception ex)
{
return false;
}
}