最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - LargeFileUploadTask in Microsoft Graph SDK Gets Stuck Without Progress or Exception - Stack Overflow

programmeradmin1浏览0评论

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;
        }
    }
发布评论

评论列表(0)

  1. 暂无评论