【问题标题】:How to upload more than 4MB in c# using Microsoft Graph API rest calls如何使用 Microsoft Graph API 休息调用在 c# 中上传超过 4MB
【发布时间】:2020-04-28 16:35:53
【问题描述】:

我没有将超过 4MB 的文件上传到 OneDrive,我已经成功创建文件夹、重命名、删除,甚至上传了一个 4MB 或更少的文件,但是上传超过 4MB 的文件似乎有点多复杂。我一直试图通过查看这个解决方案来理解 How to upload a large document in c# using the Microsoft Graph API rest calls

有两种解决方案。票数较高的解决方案建议使用冗长的方法(但是不推荐使用“GetChunkRequestResponseAsync”函数,并且我无法找到合适的函数来执行相同的操作),第二个解决方案使用“new LargeFileUpload”,但是当我把它放在我的代码(Visual Studio)中,它告诉我只有“LargeFileUploadTask ”存在(外观相同,但最后有一个“任务”。我不明白该放什么“任务 ???”。

无论如何,我绝对明白必须请求 uploadSession:

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 的链接:How to upload to OneDrive using Microsoft Graph Api in c# 谢谢

【问题讨论】:

    标签: c# file-upload microsoft-graph-api asyncfileupload


    【解决方案1】:

    自问题发布以来,Microsoft 大大简化了大文件的上传。我目前正在使用 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;
        }
    

    【讨论】:

    • 这很有帮助,解决了我的问题。我注意到的是,在我的“小于 4MB 上传”脚本中,它使用“Stream stream = new MemoryStream(byte [])”,这里我说的是“Stream stream = new FileStream(byte [])”,所以基本上它需要是文件流。然后使用:“var largeFileUpload = new LargeFile.....”(整行)和“UploadResult upload Result = await....”(整行)我主要强调哪些行对未来很重要参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多