.NET Graph SDK-OneDrive文件上传失败,并显示“不支持的段类型" [英] .NET Graph SDK - OneDrive File Upload Fails with "Unsupported segment type"
问题描述
尝试使用.NET SDK for Microsoft Graph上传文件.这是代码:
Trying to upload file using the .NET SDK for Microsoft Graph. Here is the code:
DriveItem file = new DriveItem()
{
File = new Microsoft.Graph.File(),
Name = filename,
ParentReference = new ItemReference()
{
DriveId = parent.ParentReference.DriveId,
Path = path + "/" + filename
}
};
var freq = _client
.Me
.Drive
.Items[parent.Id]
.Children
.Request();
// add the drive item
file = await freq.AddAsync(file);
DriveItem uploadedFile = null;
using (MemoryStream stream = new MemoryStream(data))
{
var req = _client
.Me
.ItemWithPath(path + "/" + file.Name)
.Content
.Request();
stream.Position = 0;
// upload the content to the driveitem just created
try
{
uploadedFile = await req.PutAsync<DriveItem>(stream);
}
catch(Exception ex)
{
Debug.WriteLine("File Put Error"); <<< FAILS HERE
}
}
return uploadedFile;
req.PutAsync方法引发异常,以上传包含文件内容的字节数组.我只是用一个小于100个字节的简单文本文件进行测试.异常包含错误的请求和不支持的细分类型.
An exception is thrown on the req.PutAsync method to upload the byte array containing the file contents. I am just testing with a simple text file, less than 100 bytes in size. The exception contains Bad Request and Unsupported segment type.
该文件是在OneDrive中创建的,但包含0个字节.
The file is created in OneDrive, but contains 0 bytes.
推荐答案
Me.ItemWithPath()需要/me后的完整路径.例如,_client.Me.ItemWithPath("/drives/driveId/items/itemId:/file/path").通过该方法,可以将通过API返回的ItemReference上返回的Path传递给ItemWithPath方法,而无需进行任何处理.
Me.ItemWithPath() requires the full path after /me. For example, _client.Me.ItemWithPath("/drives/driveId/items/itemId:/file/path"). This method is so the Path returned on the ItemReference returned via the API can be passed into the ItemWithPath method without any processing.
您要使用的是:
var req = _client
.Me
.Drive
.ItemWithPath(path + "/" + file.Name)
.Content
.Request();
或:
var req = _client
.Me
.ItemWithPath(file.ParentReference.Path + "/" + file.Name)
.Content
.Request();
这篇关于.NET Graph SDK-OneDrive文件上传失败,并显示“不支持的段类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!