Microsoft Graph API Onedrive for business 中的文件上传
Microsoft Graph API File upload in Onedrive for business
我正在使用下面的 Microsoft Graph API 代码将文件上传到当前登录用户的 OneDrive for business。
该代码可以很好地上传记事本 .txt 文件,我可以按原样正确打开文件。
但是当它上传 .docx(word 文档)时,在打开尝试时它会抛出文件损坏的错误。
我在这里缺少什么?
使用的参考 -
https://blog.mastykarz.nl/2-practical-tips-office-365-group-files-api/
https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_uploadcontent
代码:
byte[] filebytes= fileuploadControl.FileBytes;
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Put, "https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Add("Accept", "application/json;odata.metadata=verbose");
request.Content = new StringContent(DecodeFrom64(Convert.ToBase64String(filebytes)),System.Text.Encoding.ASCII, "text/plain");
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
lblFileUpload.Text = "File uploaded successfully";
}
}
}
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
鉴于您使用的是 c#,我认为您不需要先对内容进行 base64 解码。您是否尝试过删除解码部分并仅针对请求对其进行编码?
下面的代码非常适合我。
代码
var fileUrl = new Uri("https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content");
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(fileUrl);
request.Method = "PUT";
request.ContentLength = filebytes.Length;
request.AllowWriteStreamBuffering = true;
//request.Accept = "application/json;odata=verbose";
request.ContentType = "text/plain";
request.Headers.Add("Authorization", "Bearer " + accessToken);
System.IO.Stream stream = request.GetRequestStream();
//filestream.CopyTo(stream);
stream.Write(filebytes, 0, filebytes.Length);
stream.Close();
System.Net.WebResponse response = request.GetResponse();
我正在使用下面的 Microsoft Graph API 代码将文件上传到当前登录用户的 OneDrive for business。 该代码可以很好地上传记事本 .txt 文件,我可以按原样正确打开文件。 但是当它上传 .docx(word 文档)时,在打开尝试时它会抛出文件损坏的错误。 我在这里缺少什么? 使用的参考 - https://blog.mastykarz.nl/2-practical-tips-office-365-group-files-api/ https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_uploadcontent
代码:
byte[] filebytes= fileuploadControl.FileBytes;
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Put, "https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Add("Accept", "application/json;odata.metadata=verbose");
request.Content = new StringContent(DecodeFrom64(Convert.ToBase64String(filebytes)),System.Text.Encoding.ASCII, "text/plain");
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
lblFileUpload.Text = "File uploaded successfully";
}
}
}
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
鉴于您使用的是 c#,我认为您不需要先对内容进行 base64 解码。您是否尝试过删除解码部分并仅针对请求对其进行编码?
下面的代码非常适合我。 代码
var fileUrl = new Uri("https://graph.microsoft.com/v1.0/me/drive/root/children/" + filename + "/content");
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(fileUrl);
request.Method = "PUT";
request.ContentLength = filebytes.Length;
request.AllowWriteStreamBuffering = true;
//request.Accept = "application/json;odata=verbose";
request.ContentType = "text/plain";
request.Headers.Add("Authorization", "Bearer " + accessToken);
System.IO.Stream stream = request.GetRequestStream();
//filestream.CopyTo(stream);
stream.Write(filebytes, 0, filebytes.Length);
stream.Close();
System.Net.WebResponse response = request.GetResponse();