简体   繁体   English

如何使用 Microsoft Graph API rest 调用在 c# 中上传超过 4MB

[英]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.

相关问题 如何使用 Microsoft Graph API rest 调用在 c# 中上传大型文档 - How to upload a large document in c# using the Microsoft Graph API rest calls 使用 Graph API 将文件 (&gt; 4MB) 上传到 OneDrive - Upload file (> 4MB) to OneDrive using Graph API 如何使用C#将字符串内容限制为小于4MB并将该字符串保存在DB中 - How to restrict a content of string to less than 4MB and save that string in DB using C# 如何在 c# 中使用 Microsoft Graph Api 上传到 OneDrive - How to upload to OneDrive using Microsoft Graph Api in c# Microsoft Graph - .NET SDK - OneDrive 大文件上传(&gt; 4MB 大小) - Microsoft Graph - .NET SDK - OneDrive Large File Upload (> 4MB in size) 如何使用MVC C#对Microsoft图进行REST Api调用? - How to make a REST Api call to Microsoft graph using MVC C#? C# Azure AppendBlob AppendBlock 添加大于 4mb 限制的文件 - C# Azure AppendBlob AppendBlock adding a file larger than the 4mb limit 如何使用 C# 调用 REST API? - How do I make calls to a REST API using C#? Microsoft Graph Rest API使用c#ASP.NET MVC将附件添加到电子邮件 - Microsoft Graph Rest API Add attachment to email using c# ASP.NET MVC 使用Graph API使用C#上传到Onedrive - Upload to Onedrive with C# using Graph API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM