How to upload a large document in c# using the Microsoft Graph API rest calls(如何使用 Microsoft Graph API 休息调用在 c# 中上传大型文档)
问题描述
I am using an external .Net Web App and would like to know how to upload a large file to the document library using Microsoft Graph. I am able to upload up to 4mb but anything above it is throwing an error.
I know there is a createUploadSession
however not sure how to implement it. Any help would greatly appreciate.
This is what I am doing to upload up to 4mb successfully:
string requestUrl =
"https://graph.microsoft.com/v1.0/drives/{mydriveid}/items/root:/" +
fileName + ":/content";
HttpClient Hclient = new HttpClient();
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, requestUrl);
message.Content = new StreamContent(file.InputStream);
client.DefaultRequestHeaders
.TryAddWithoutValidation("Content-Type",
"application/json; odata=verbose; charset=utf-8");
HttpResponseMessage Hresponse = await client.SendAsync(message);
//if the response is 200 then read the response and retrive the GUID!
if (Hresponse.IsSuccessStatusCode)
{
responseString = await
Hresponse.Content.ReadAsStringAsync();
JObject jDataRetrieved = JObject.Parse(responseString);
strGuid = jDataRetrieved.SelectToken("eTag").ToString();
}
New and improved large file upload for the .NET client library
With the fluent client
Create upload session
// Create upload session
// POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession
var uploadSession = await graphClient.Drive.Items[itemId].ItemWithPath("SWEBOK.pdf").CreateUploadSession().Request().PostAsync();
Create the task
// Create task
var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default.
var largeFileUpload = new LargeFileUpload(uploadSession, graphClient, stream, maxChunkSize);
Create upload monitor
public class MyProgress : IProgressCallback
{
public void OnFailure(ClientException clientException)
{
Console.WriteLine(clientException.Message);
}
public void OnSuccess(DriveItem result)
{
Console.WriteLine("Download completed with id below");
Console.WriteLine(result.Id);
}
public void UpdateProgress(long current, long max)
{
long percentage = (current * 100) / max ;
Console.WriteLine("Upload in progress. " + current + " bytes of " + max + "bytes. " + percentage + " percent complete");
}
}
Upload the file
uploadedFile = await largeFileUpload.ResumeAsync(new MyProgress());
With the HTTP client
Create upload session
// Create upload session
// POST /v1.0/drive/items/01KGPRHTV6Y2GOVW7725BZO354PWSELRRZ:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession
string uri = $"https://graph.microsoft.com/v1.0/drive/items/{itemId}:/SWEBOKv3.pdf:/microsoft.graph.createUploadSession";
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(httpRequestMessage);
// Read the session info from the response
var httpResponseMessage = await graphClient.HttpProvider.SendAsync(httpRequestMessage);
var content = await httpResponseMessage.Content.ReadAsStringAsync();
var uploadSession = graphClient.HttpProvider.Serializer.DeserializeObject<UploadSession>(content);
Create the task
// Create task
var maxSliceSize = 320 * 1024; // 320 KB - Change this to your chunk size. 4MB is the default.
LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, stream, maxSliceSize);
Create upload monitor
// Setup the progress monitoring
IProgress<long> progress = new Progress<long>(progress =>
{
Console.WriteLine($"Uploaded {progress} bytes of {stream.Length} bytes");
});
Upload the file
UploadResult<DriveItem> uploadResult = null;
try
{
uploadResult = await largeFileUploadTask.UploadAsync(progress);
if (uploadResult.UploadSucceeded)
{
Console.WriteLine($"File Uploaded {uploadResult.ItemResponse.Id}");//Sucessful Upload
}
}
catch (ServiceException e)
{
Console.WriteLine(e.Message);
}
这篇关于如何使用 Microsoft Graph API 休息调用在 c# 中上传大型文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Microsoft Graph API 休息调用在 c# 中上传大型文档
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01