如何在C#中使用Microsoft Graph Api上传到OneDrive [英] How to upload to OneDrive using Microsoft Graph Api in c#

查看:283
本文介绍了如何在C#中使用Microsoft Graph Api上传到OneDrive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图上载到OneDrive帐户,但我无法上载不能上传少于或大于4MB的文件.我完全没有访问驱动器的问题,因为我具有创建文件夹,重命名文件/文件夹和删除文件/文件夹的工作功能.

I have been trying to upload to a OneDrive account and I am hopelessly stuck not being able to upload neither less or greater than 4MB files. I have no issues accessing the drive at all, since I have working functions that create a folder, rename files/folders, and a delete files/folders.

此关于Microsoft Graph API的文档对HTTP代码非常友好,我相信我能够将文档合理地翻译"为C#,但仍然无法抓取文件并将其上传到OneDrive.在线上的某些地方似乎正在使用字节数组?我完全不熟悉它,因为我的主要语言是C ++,我们只使用ifstream/ofstream.无论如何,这是特定代码的一部分(我希望这已经足够了):

This documentation on Microsoft Graph API is very friendly to HTTP code, and I believe I am able to fairly "translate" the documentation to C#, but still fail to grab a file and upload to OneDrive. Some places online seem to be using byte arrays? Which I am completely unfamiliar with since my primary language is C++ and we just use ifstream/ofstream. Anyways, here is the portion of code in specific (I hope this is enough):

var item = await _client.Users[userID].Drive.Items[FolderID]//"01YZM7SMVOQ7YVNBXPZFFKNQAU5OB3XA3K"].Content
                    .ItemWithPath("LessThan4MB.txt")//"D:\\LessThan4MB.txt")
                    .CreateUploadSession()
                    .Request()
                    .PostAsync();
            Console.WriteLine("done printing");

按现状显示,它会在OneDrive中上载一个带有波浪号〜"的临时文件(就像我只能打开文件但不能从文件中导入任何数据一样).如果我交换文件名,使其包含文件位置,则会抛出错误:

As it stands, it uploads a temporary file that has a tilde "~" in the OneDrive (like as if I was only able to open but not import any data from the file onto it). If I swap the name of the file so it includes the file location it throws an error:

消息:在一个打开的属性上找到了一个函数"microsoft.graph.createUploadSession".不支持有关开放属性的功能.

Message: Found a function 'microsoft.graph.createUploadSession' on an open property. Functions on open properties are not supported.

谢谢,任何评论和帮助都将很重要.

Thanks, any comments and help would mean a lot.

推荐答案

使用内存流和PutAsync<DriveItem>请求尝试这种方法:

Try this approach with memory stream and PutAsync<DriveItem> request:

string path = "D:\\LessThan4MB.txt";
byte[] data = System.IO.File.ReadAllBytes(path);

using (Stream stream = new MemoryStream(data))
{
    var item = await _client.Me.Drive.Items[FolderID]
            .ItemWithPath("LessThan4MB.txt")
            .Content
            .Request()
            .PutAsync<DriveItem>(stream);
}

我假设您已经授予Microsoft Graph Files.ReadWrite.All 权限.检查您的 API权限.我使用相当老的Microsoft.Graph库1.21.0版测试了此代码段.希望它也对您有用.

I am assuming you have already granted Microsoft Graph Files.ReadWrite.All permission. Check your API permission. I tested this code snippet with pretty old Microsoft.Graph library version 1.21.0. Hopefully it will work for you too.

这篇关于如何在C#中使用Microsoft Graph Api上传到OneDrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆