0

在Microsoft Graph OneDrive 团队处理本教程Uploading File to OneDrive时,我在下面显示的代码的最后一行收到以下错误:

备注:网上有一些帖子有相关问题,(如:这个,或这个,或这个这个这个)。但他们似乎都有不同的背景或没有回应。

问题:可能是什么问题,我们如何解决它

未找到段“根:”的资源

相关代码

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);
}
4

1 回答 1

2

.ItemWithPath(file.Path)不是您要上传的文件的路径,而是目标路径。

例如,如果您想将“SomeFile.txt”上传到 OneDrive 的根目录,您可以使用:

graphClient.Me.Drive // The drive
  .Root // The drive's root folder
  .ItemWithPath("SomeFile.txt") // The destination to write the upload to

目前失败的原因是 OneDrive 不知道如何处理 Windows 驱动器路径(即C:\Files\Documents\SomeFile.txt)。它需要一个 URL 安全驱动器路径(即/Documents/SomeFile.txt)。

于 2020-07-13T20:24:05.653 回答