[英]Upload file to OneDrive using RestAPI
I am trying to upload an image to OneDrive using below code. 我正在尝试使用以下代码将图像上传到OneDrive。 The file was successfully uploaded to the OneDrive folder but when I download the file manually from OneDrive, it opens in black color and shows Invalid Image.
该文件已成功上载到OneDrive文件夹,但是当我从OneDrive手动下载文件时,它将以黑色打开并显示无效图像。
var client = new RestClient("https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content");
var request = new RestRequest(Method.PUT);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", Path.GetExtension(originalFileName).GetMimeType());
request.AddHeader("Authorization", "Bearer " + GetAccessToken());
request.AddFile("content", System.IO.File.ReadAllBytes(filePath), originalFileName);
var response = client.Execute(request);
I really do not know what mistake I am making in here. 我真的不知道我在这里犯了什么错误。 May you please help me?
你能帮我吗?
Inspired from this SO answer 灵感来自这个SO答案
I need to change it to HttpClient
from RestClient
. 我需要从
RestClient
将其更改为HttpClient
。 After change the code will like: 更改后代码将如下:
using (var client = new HttpClient())
{
var url = "https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content";
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + GetAccessToken());
byte[] sContents = System.IO.File.ReadAllBytes(filePath);
var content = new ByteArrayContent(sContents);
var response = client.PutAsync(url, content).Result.Content.ReadAsStringAsync().Result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.