none
Upload file to OneDrive using RestAPI RRS feed

  • Question

  • 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


    Monday, May 20, 2019 5:49 AM

Answers

  • Hi Mohamed,

    Thank you for posting here.

    Based on your description, you want to solve the exception 'the file was corrupt' after you upload the file to OneDrive by using RestAPI.

    I find the following link is similar to your problem, you could have a look.

    https://stackoverflow.com/questions/39214692/upload-to-onedrive-with-c-sharp-using-graph-api

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 8:47 AM
    Moderator
  • Thanks much. Inspired from this [SO answer][1]

    I need to change it to `HttpClient` from `RestClient`. After change the code will like:

    using (var client = new HttpClient())
    {
    var url = "https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content";
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + GetAccessToken());

    byte[] sContents = System.IO.File.ReadAllBytes(filePath);
    var content = new ByteArrayContent(sContents);

    var response = client.PutAsync(url, content).Result.Content.ReadAsStringAsync().Result;
    }


      [1]: https://stackoverflow.com/a/39242097/3682161

    - Mohamed Thaufeeq A

    Tuesday, May 21, 2019 6:18 AM

All replies

  • Hi Mohamed,

    Thank you for posting here.

    Based on your description, you want to solve the exception 'the file was corrupt' after you upload the file to OneDrive by using RestAPI.

    I find the following link is similar to your problem, you could have a look.

    https://stackoverflow.com/questions/39214692/upload-to-onedrive-with-c-sharp-using-graph-api

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 8:47 AM
    Moderator
  • Thanks much. Inspired from this [SO answer][1]

    I need to change it to `HttpClient` from `RestClient`. After change the code will like:

    using (var client = new HttpClient())
    {
    var url = "https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content";
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + GetAccessToken());

    byte[] sContents = System.IO.File.ReadAllBytes(filePath);
    var content = new ByteArrayContent(sContents);

    var response = client.PutAsync(url, content).Result.Content.ReadAsStringAsync().Result;
    }


      [1]: https://stackoverflow.com/a/39242097/3682161

    - Mohamed Thaufeeq A

    Tuesday, May 21, 2019 6:18 AM