nam
nam

Reputation: 23749

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

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

Upvotes: 0

Views: 5526

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33094

.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).

Upvotes: 2

Related Questions