Microsoft graph api .NET Graph SDK-OneDrive文件上载失败,返回“0”;“不支持的段类型”;
正在尝试使用.NET SDK for Microsoft Graph上载文件。代码如下:Microsoft graph api .NET Graph SDK-OneDrive文件上载失败,返回“0”;“不支持的段类型”;,microsoft-graph-api,Microsoft Graph Api,正在尝试使用.NET SDK for Microsoft Graph上载文件。代码如下: DriveItem file = new DriveItem() { File = new Microsoft.Graph.File(), Name = filename, ParentReference = new ItemReference() { Drive
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;
DriveItem文件=新的DriveItem()
{
File=新的Microsoft.Graph.File(),
Name=文件名,
ParentReference=新项目引用()
{
DriveId=parent.ParentReference.DriveId,
路径=路径+“/”+文件名
}
};
var freq=\u客户端
我
.开车
.Items[parent.Id]
儿童
.Request();
//添加驱动器项
file=await freq.AddAsync(文件);
DriveItem uploadedFile=null;
使用(内存流=新内存流(数据))
{
var req=\u客户端
我
.ItemWithPath(路径+“/”+文件名)
.内容
.Request();
流位置=0;
//将内容上载到刚刚创建的driveitem
尝试
{
uploadedFile=wait req.PutAsync(流);
}
捕获(例外情况除外)
{
Debug.WriteLine(“File Put Error”);Me.ItemWithPath()需要/Me之后的完整路径。例如,_client.Me.ItemWithPath(“/drives/driveId/items/itemId:/File/path”)。此方法可以将通过API返回的ItemReference上返回的路径传递到ItemWithPath方法中,而无需任何处理
您要使用的是:
var req = _client
.Me
.Drive
.ItemWithPath(path + "/" + file.Name)
.Content
.Request();
或:
Me.ItemWithPath()需要/Me之后的完整路径。例如,_client.Me.ItemWithPath(“/drives/driveId/items/itemId:/file/path”)。此方法非常简单,因此通过API返回的ItemReference上返回的路径可以传递到ItemWithPath方法,而无需任何处理
您要使用的是:
var req = _client
.Me
.Drive
.ItemWithPath(path + "/" + file.Name)
.Content
.Request();
或:
我发现有时跳过在SDK语句中设置包含文件夹id的路径更容易…适用于OneDrive和统一组
var createdFile = await graphClient.Me.Drive
.Items[currentDriveFolder.id]
.ItemWithPath(fileName)
.Content.Request()
.PutAsync<DriveItem>(stream);
var createdFile=wait graphClient.Me.Drive
.Items[currentDriveFolder.id]
.ItemWithPath(文件名)
.Content.Request()
.PutAsync(流);
我真的希望能够像这样设置驱动器id和文件夹id:
var createdFile = await graphClient
.Drives[driveId]
.Items[folderId]
.ItemWithPath(fileName)
.Content
.Request()
.PutAsync<DriveItem>(stream);
var createdFile=Wait graphClient
.Drives[driveId]
.项目[folderId]
.ItemWithPath(文件名)
.内容
.Request()
.PutAsync(流);
我发现有时跳过SDK语句中设置包含文件夹id的路径更容易…适用于OneDrive和统一组
var createdFile = await graphClient.Me.Drive
.Items[currentDriveFolder.id]
.ItemWithPath(fileName)
.Content.Request()
.PutAsync<DriveItem>(stream);
var createdFile=wait graphClient.Me.Drive
.Items[currentDriveFolder.id]
.ItemWithPath(文件名)
.Content.Request()
.PutAsync(流);
我真的希望能够像这样设置驱动器id和文件夹id:
var createdFile = await graphClient
.Drives[driveId]
.Items[folderId]
.ItemWithPath(fileName)
.Content
.Request()
.PutAsync<DriveItem>(stream);
var createdFile=Wait graphClient
.Drives[driveId]
.项目[folderId]
.ItemWithPath(文件名)
.内容
.Request()
.PutAsync(流);
这项工作做得很好,它还具有在一个步骤中创建文件和上载内容的优势。这项工作做得很好,它还具有在一个步骤中创建文件和上载内容的优势。ItemWithPath真的是驱动器的属性吗?还是说_client.Me.Drive.Items[“{driveItem Id}”].ItemWithPath(…?ItemWithPath真的是驱动器的属性吗?还是说_client.Me.Drive.Items[“{driveItem Id}”]。ItemWithPath(?