0
votes

I am using Onedrive Rest API to Upload a file into my Onedrive Account. Below is the mentioned Microsoft documentation link to the Upload file.

https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online

Whenever I used the above API the file gets uploaded into my account but the file gets corrupted.

Below mentioned is my request object.

{
method: "PUT",
url: Upload Url,
processData: false,
headers: {
     "Authorization": <access_token> 
     "Content-Disposition": 'form-data; name="metadata"',
     "Content-Type": "application/json; charset=UTF-8",
     "Content-Transfer-Encoding": "8bit"
    },
formData: {
file: {
        value: fs.createReadStream("Smile.png"),
        options:
          {
            filename: "Smile.png,
            contentType: null
          }
       }
      }
}

The file gets uploaded in the proper folder but it's corrupted and I am unable to view it in my Onedrive Account. Can someone please help me with this.

1
Did you find any solution for this? I am facing same problem.user1398291
Did you solve it? I am the same problem.김세림
I also have the same problem. Please give us the solutionFreezer freezer

1 Answers

0
votes

The problem lies in how you pass the data in the body. I had the same issue and solved by passing directly the image buffer (in your case fs.createReadStream("Smile.png") as a body (without any curly brackets {})

My code:

    const config = {
        headers: { Authorization: `Bearer ${token}`,
        }
    };

    const bodyParameters = imageBuffer;
    await axios.put("https://graph.microsoft.com/v1.0/drives/{drive-id}/items/{item-id}:/{filename}:/content",bodyParameters,config)