Onedrive上载API上载损坏的文件或图像

我正在使用Onedrive Rest API将文件上传到我的Onedrive帐户中。下面是提到的指向上载文件的microsoft文档链接。

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

每当我使用上述API时,文件都会上传到我的帐户中,但文件已损坏。

下面提到的是我的请求对象。

{
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
          }
       }
      }
}

该文件已上传到正确的文件夹中,但已损坏,无法在我的Onedrive帐户中查看。 有人可以帮我吗?

wangzywangzy 回答:Onedrive上载API上载损坏的文件或图像

问题在于你如何在body中传递数据。我遇到了同样的问题,并通过直接传递图像缓冲区(在您的情况下 fs.createReadStream("Smile.png") 作为正文(没有任何大括号 {})

我的代码:

    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)
本文链接:https://www.f2er.com/3154870.html

大家都在问