我没有运气上传超过4MB的文件到OneDrive,我已经成功地创建了文件夹,重命名,删除,甚至上传了一个4MB或更少的文件,然而上传超过4MB的东西似乎有点复杂。我一直在尝试通过看这个解决方案来理解如何在c#中使用Microsoft Graph API休眠调用上传大型文档
有两种解决方案。票数较高的解决方案建议使用一个冗长的方法(然而 "GetChunkRequestResponseAsync "这个函数已经被废弃了,我也没有找到合适的函数来执行同样的功能),第二个解决方案使用 "new LargeFileUpload",然而当我把这个放在我的代码中(Visual Studio)时,它告诉我只有 "LargeFileUploadTask < ? > "存在(同样的函数从外观上看然而最后有一个 "Task"。我不明白 "Task < string > ? "该怎么写。
总之,我绝对明白,一个uploadSession必须是Requested。
var uploadSession = await _client.Drive.Items[FolderID]
.ItemWithPath(Name)
.CreateUploadSession()
.Request()
.PostAsync();
var maxChunkSize = 320 * 1024; //320 KB chunk sizes
而且可能会涉及到把数据存储到一个字节数组中,比如说。
string FilePath = "D:\\MoreThan5MB.txt";
string path = FilePath;//Actual File Location in your hard drive
byte[] data = System.IO.File.ReadAllBytes(path); //Stores all data into byte array by name of "data" then "PUT to the root folder
Stream stream = new MemoryStream(data);
但是任何帮助和建议都将是非常感激的。如果这意味着什么,这里有一个链接,有人帮我上传小于4MB的数据。如何在c#中使用微软Graph Api上传到OneDrive中? 谢谢你
微软已经使大文件的上传明显容易,因为帖子的问题。我目前使用的是MS Graph 1.21.0。这段代码只要稍作调整就可以了。
public async Task<DriveItem> UploadToFolder(
string driveId,
string folderId,
string fileLocation,
string fileName)
{
DriveItem resultDriveItem = null;
using (Stream fileStream = new FileStream(
fileLocation,
FileMode.Open,
FileAccess.Read))
{
var uploadSession = await _graphServiceClient.Drives[driveId].Items[folderId]
.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync();
int maxSlice = 320 * 1024;
var largeFileUpload = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxSlice);
IProgress<long> progress = new Progress<long>(x =>
{
_logger.LogDebug($"Uploading large file: {x} bytes of {fileStream.Length} bytes already uploaded.");
});
UploadResult<DriveItem> uploadResult = await largeFileUpload.UploadAsync(progress);
resultDriveItem = uploadResult.ItemResponse;
}
return resultDriveItem;
}