Microsoft Graph Error - Resource not found for the segment 'root:' when uploading file to OneDrive

自闭症网瘾萝莉.ら 提交于 2020-07-31 05:24:33

问题


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

回答1:


.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:\Files\Documents\SomeFile.txt). It expects a URL safe drive path (i.e. /Documents/SomeFile.txt).



来源:https://stackoverflow.com/questions/62725442/microsoft-graph-error-resource-not-found-for-the-segment-root-when-uploadin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!