本篇,我们将介绍使用 Microsoft Graph SDK 的 LargeFileUploadTask 来上传大文件 (大于 4M 的文件)。
随着 Microsoft Graph SKD 的更新,ChunkedUploadProvider 已经过时了,当前可以使用 LargeFileUploadTask 来上传大文件。示例代码如下:
using (var fileStream = System.IO.File.OpenRead(filePath))
{
var uploadProps = new DriveItemUploadableProperties
{
ODataType = null,
AdditionalData = new Dictionary<string, object>
{
{ "@microsoft.graph.conflictBehavior", "replace" }
}
};
var uploadSession = await graphClient.Me.Drive.Root
.ItemWithPath(itemPath)
.CreateUploadSession(uploadProps)
.Request()
.PostAsync();
int maxSliceSize = 320 * 1024;
var fileUploadTask =
new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxSliceSize);
IProgress<long> progress = new Progress<long>(prog => {
Console.WriteLine($"Uploaded {prog} bytes of {fileStream.Length} bytes");
});
try
{
var uploadResult = await fileUploadTask.UploadAsync(progress);
if (uploadResult.UploadSucceeded)
{
Console.WriteLine($"Upload complete, item ID: {uploadResult.ItemResponse.Id}");
}
else
{
Console.WriteLine("Upload failed");
}
}
catch (ServiceException ex)
{
Console.WriteLine($"Error uploading: {ex.ToString()}");
}
}
更多信息可参阅 Microsoft Docs文档 Upload large files using the Microsoft Graph SDKs
|