.NET Graph SDK - OneDrive 文件上传失败 "Unsupported segment type"
.NET Graph SDK - OneDrive File Upload Fails with "Unsupported segment type"
正在尝试使用 Microsoft Graph 的 .NET SDK 上传文件。这是代码:
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 字节的简单文本文件进行测试。异常包含 Bad Request 和 Unsupported segment type。
该文件是在 OneDrive 中创建的,但包含 0 个字节。
Me.ItemWithPath() 需要 /me 之后的完整路径。例如,_client.Me.ItemWithPath("/drives/driveId/items/itemId:/file/path")。通过此方法,可以将通过 API 返回的 ItemReference 上返回的 Path 传递到 ItemWithPath 方法中,而无需任何处理。
您要使用的是:
var req = _client
.Me
.Drive
.ItemWithPath(path + "/" + file.Name)
.Content
.Request();
或:
var req = _client
.Me
.ItemWithPath(file.ParentReference.Path + "/" + file.Name)
.Content
.Request();
我发现有时在 SDK 语句中跳过设置包含文件夹 ID 的路径更容易...适用于 OneDrive 和统一组..
var createdFile = await graphClient.Me.Drive
.Items[currentDriveFolder.id]
.ItemWithPath(fileName)
.Content.Request()
.PutAsync<DriveItem>(stream);
我真的很希望能够像这样设置驱动器 ID 和文件夹 ID:
var createdFile = await graphClient
.Drives[driveId]
.Items[folderId]
.ItemWithPath(fileName)
.Content
.Request()
.PutAsync<DriveItem>(stream);
正在尝试使用 Microsoft Graph 的 .NET SDK 上传文件。这是代码:
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 字节的简单文本文件进行测试。异常包含 Bad Request 和 Unsupported segment type。
该文件是在 OneDrive 中创建的,但包含 0 个字节。
Me.ItemWithPath() 需要 /me 之后的完整路径。例如,_client.Me.ItemWithPath("/drives/driveId/items/itemId:/file/path")。通过此方法,可以将通过 API 返回的 ItemReference 上返回的 Path 传递到 ItemWithPath 方法中,而无需任何处理。
您要使用的是:
var req = _client
.Me
.Drive
.ItemWithPath(path + "/" + file.Name)
.Content
.Request();
或:
var req = _client
.Me
.ItemWithPath(file.ParentReference.Path + "/" + file.Name)
.Content
.Request();
我发现有时在 SDK 语句中跳过设置包含文件夹 ID 的路径更容易...适用于 OneDrive 和统一组..
var createdFile = await graphClient.Me.Drive
.Items[currentDriveFolder.id]
.ItemWithPath(fileName)
.Content.Request()
.PutAsync<DriveItem>(stream);
我真的很希望能够像这样设置驱动器 ID 和文件夹 ID:
var createdFile = await graphClient
.Drives[driveId]
.Items[folderId]
.ItemWithPath(fileName)
.Content
.Request()
.PutAsync<DriveItem>(stream);