Microsoft Graph 错误 - 将文件上传到 OneDrive 时找不到段“根:"的资源 [英] Microsoft Graph Error - Resource not found for the segment 'root:' when uploading file to OneDrive
问题描述
在处理 this tutorial on Uploading File to OneDrive
from Microsoft Graph OneDrive 团队,我在下面显示的代码的最后一行收到以下错误:
备注:网上有些帖子有相关问题,(如:这个,或 此,或 这个 或 这个 或 这个).但他们似乎都有不同的背景或没有回应.
问题:可能是什么问题,我们如何解决它
<块引用><块引用>未找到段根:"的资源
相关代码:
GraphServiceClient graphClient = ProviderManager.Instance.GlobalProvider.Graph;var picker = new Windows.Storage.Pickers.FileOpenPicker();……picker.FileTypeFilter.Add("*");Windows.Storage.StorageFile 文件 = 等待选择器.PickSingleFileAsync();如果(文件!= null){//应用程序现在对选取的文件具有读/写权限//请求 1 - 上传小文件到用户的 onedrive流文件流 = 等待文件.OpenStreamForReadAsync();DriveItem 上传文件 = 等待 graphClient.Me.Drive.Root.ItemWithPath(file.Path).内容.要求().PutAsync(fileStream);}
.ItemWithPath(file.Path)
不是您要上传的文件的路径,而是目标路径.
例如,如果您想上传SomeFile.txt";到 OneDrive 的根目录,您将使用:
graphClient.Me.Drive//驱动器.Root//驱动器的根文件夹.ItemWithPath("SomeFile.txt")//上传文件的目的地
目前失败的原因是 OneDrive 不知道如何处理 Windows 驱动器路径(即 C:FilesDocumentsSomeFile.txt
).它需要一个 URL 安全驱动器路径(即 /Documents/SomeFile.txt
).
When working on this tutorial on Uploading File to OneDrive
from Microsoft Graph OneDrive team, I'm getting the following error at the last line of the code shown below:
Remarks: There are some posts online with a related issue, (such as: This, or this, or this or this or this). But they all seem to have a different context or do not have a response.
Question: What could be the issue, and how can we resolve it
Resource not found for the segment 'root:'
Relevant Code:
GraphServiceClient graphClient = ProviderManager.Instance.GlobalProvider.Graph;
var picker = new Windows.Storage.Pickers.FileOpenPicker();
....
picker.FileTypeFilter.Add("*");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
// request 1 - upload small file to user's onedrive
Stream fileStream = await file.OpenStreamForReadAsync();
DriveItem uploadedFile = await graphClient.Me.Drive.Root
.ItemWithPath(file.Path)
.Content
.Request()
.PutAsync<DriveItem>(fileStream);
}
.ItemWithPath(file.Path)
isn't the path to the file you're uploading, it is the destination path.
For example, if you wanted to upload "SomeFile.txt" to the root of your OneDrive, you would use:
graphClient.Me.Drive // The drive
.Root // The drive's root folder
.ItemWithPath("SomeFile.txt") // The destination to write the upload to
The reason this is currently failing is OneDrive doesn't know what to do with a Windows drive path (i.e. C:FilesDocumentsSomeFile.txt
). It expects a URL safe drive path (i.e. /Documents/SomeFile.txt
).
这篇关于Microsoft Graph 错误 - 将文件上传到 OneDrive 时找不到段“根:"的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!