[英]How to upload more than 4MB in c# using Microsoft Graph API rest calls
I haven't had luck uploading more than 4MB to a OneDrive, I have successfully Created Folder, rename, delete, and even upload a file that is 4MB or less, however uploading something more than 4MB seems to be a little more complicated.我没有将超过 4MB 的文件上传到 OneDrive,我已经成功地创建了文件夹、重命名、删除,甚至上传了一个 4MB 或更少的文件,但是上传超过 4MB 的文件似乎有点复杂。 I have been trying to understand by looking at this solution How to upload a large document in c# using the Microsoft Graph API rest calls
我一直试图通过查看此解决方案来了解如何使用 Microsoft Graph API rest 调用在 c# 中上传大型文档
There are two solutions.有两种解决方案。 The solution with higher votes suggests a lengthy method (however the function "GetChunkRequestResponseAsync" is deprecated, and I haven't been able to find a suitable function to perform the same), and the second solution uses "new LargeFileUpload", however when I put this in my code (Visual Studio), it tells me that only "LargeFileUploadTask <? > " exists (same function by appearance however has a "Task" at the end. I don't understand what to put "Task < string >???".
票数较高的解决方案建议使用冗长的方法(但是不推荐使用 function“GetChunkRequestResponseAsync”,并且我无法找到合适的 function 来执行相同的操作),但是当我第二个解决方案使用“new LargeFileUpload”时把它放在我的代码(Visual Studio)中,它告诉我只有“LargeFileUploadTask <? >”存在(外观相同的function但是最后有一个“任务”。我不明白该放什么“任务<字符串> ???”。
Anyways, I definitely understand that an uploadSession must be Requested:无论如何,我绝对理解必须请求 uploadSession:
var uploadSession = await _client.Drive.Items[FolderID]
.ItemWithPath(Name)
.CreateUploadSession()
.Request()
.PostAsync();
var maxChunkSize = 320 * 1024; //320 KB chunk sizes
and it may involve storing data into a byte array such like:它可能涉及将数据存储到字节数组中,例如:
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);
But any help and suggestions would be greatly appreciated.但任何帮助和建议将不胜感激。 If it means anything, here is a link of someone helping me upload less than 4MB: How to upload to OneDrive using Microsoft Graph Api in c# Thanks
如果这意味着什么,这里是一个帮助我上传小于 4MB 的链接: How to upload to OneDrive using Microsoft Graph Api in c#谢谢
Microsoft has made the upload of large files significantly easier, since the posts to the question.自从问题发布以来,微软使大文件的上传变得更加容易。 I am currently using MS Graph 1.21.0.
我目前正在使用 MS Graph 1.21.0。 This code should work with some small adjustments:
此代码应进行一些小的调整:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.