Upload file to OneDrive using RestAPI

  • Thread starter Thread starter Mohamed Thaufeeq
  • Start date Start date
M

Mohamed Thaufeeq

Guest
I am trying to upload an image to OneDrive using below code. The file was successfully uploaded with correct file size but is always says 'the file was corrupt' when I try to download.

Try #1

var client = new RestClient("https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content");
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", Path.GetExtension(originalFileName).GetMimeType());
request.AddHeader("Authorization", "Bearer " + GetAccessToken());
request.AddFile("content", System.IO.File.ReadAllBytes(filePath), originalFileName);

var response = client.Execute(request);

Try #2

var client = new RestClient("https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content");
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", "Bearer " + GetAccessToken());
request.AddFile("content", filePath);

var response = client.Execute(request);

The file was successfully uploaded with correct file size but is always says 'the file was corrupt' when I try to download.

I really do not know what mistake I am making in here. May you please help me?


- Mohamed Thaufeeq A

Continue reading...
 
Back
Top