尝试将尚不存在的文件上传到 onedrive。我已经设法让它更新现有文件。但似乎无法弄清楚如何创建一个全新的文件。我已经使用以下方法完成了此操作Microsoft.Graph
图书馆。
以下是用于更新现有文件的代码:
public async Task<ActionResult> OneDriveUpload()
{
string token = await GetAccessToken();
if (string.IsNullOrEmpty(token))
{
// If there's no token in the session, redirect to Home
return Redirect("/");
}
GraphServiceClient client = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);
return Task.FromResult(0);
}));
try
{
string path = @"C:/Users/user/Desktop/testUpload.xlsx";
byte[] data = System.IO.File.ReadAllBytes(path);
Stream stream = new MemoryStream(data);
// Line that updates the existing file
await client.Me.Drive.Items["55BBAC51A4E4017D!104"].Content.Request().PutAsync<DriveItem>(stream);
return View("Index");
}
catch (ServiceException ex)
{
return RedirectToAction("Error", "Home", new { message = "ERROR retrieving messages", debug = ex.Message });
}
}
我建议使用ChunkedUploadProvider https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/40359c6d7029c54d8e9a6a2a1a9dbb4e936ed037/src/Microsoft.Graph/Requests/Helpers/ChunkedUploadProvider.csSDK 中包含的实用程序。除了更容易使用之外,它还允许您上传任意一侧的文件,而不是仅限于 4MB 以下的文件。
您可以找到如何使用的示例ChunkedUploadProvider
in the OneDriveUploadLargeFile https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/2d565c10969689344bbdaa58f4ab74df06303063/tests/Microsoft.Graph.Test/Requests/Functional/OneDriveTests.cs#L39单元测试。
要回答您的直接问题,上传对于替换和创建文件的作用是相同的。但是,您确实需要指定文件名而不仅仅是现有的项目编号:
await graphClient.Me
.Drive
.Root
.ItemWithPath("fileName")
.Content
.Request()
.PutAsync<DriveItem>(stream);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)