我正在使用外部 .Net Web 应用程序,想知道如何使用 Microsoft Graph 将大文件上传到文档库。我最多可以上传 4mb,但任何超过它的内容都会引发错误。
我知道有一个 createUploadSession
但不确定如何实现它。任何帮助将不胜感激。
这就是我为成功上传最多 4mb 所做的操作:
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();
}
最佳答案
.NET 客户端库的新的和改进的大文件上传
使用流畅的客户端
创建上传 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 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);
创建上传监视器
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");
}
}
上传文件
uploadedFile = await largeFileUpload.ResumeAsync(new MyProgress());
使用 HTTP 客户端
创建上传 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 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);
创建上传监视器
// Setup the progress monitoring
IProgress<long> progress = new Progress<long>(progress =>
{
Console.WriteLine($"Uploaded {progress} bytes of {stream.Length} bytes");
});
上传文件
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);
}
关于c# - 如何使用 Microsoft Graph API rest 调用在 c# 中上传大型文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776955/