无法使用OneDrive for Business API将内容放入/上传到OneDrive For Business中的文件

时间:2016-02-25 10:53:28

标签: javascript onedrive

我正在尝试使用OneDrive for Business REST API将内容添加到OneDrive for Business中的文件。 以下是代码段:

 var getFile = getFileBuffer(file);
  getFile.done(function (arrayBuffer) {
    var content = arrayBuffer;
    var query = "https://myonedrive/_api/v2.0/drive/{driveId}/items/{parentfolderId}/children/{fileName}/content";
  $.ajax({
      url: query,
      method: "PUT",
      data: arrayBuffer,   
      headers: {
      "accept": "application/json;odata=verbose",
      "content-length": arrayBuffer.byteLength
      },
      beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", "Bearer " + bearerTokenvalue);
           },
 success: function (data) {
          return
       },
 error: function (err) {
            return;
        }
    })
  });
function getFileBuffer(file) {
    var deferred = jQuery.Deferred();
    var reader = new FileReader();
    reader.onloadend = function (e) {
        deferred.resolve(e.target.result);
    }
    reader.onerror = function (e) {
        deferred.reject(e.target.error);
    }
    reader.readAsArrayBuffer(file);
    return deferred.promise();
}

我在成功通话中收到以下错误,内容未上传到文件中。

ErrorMessage

1 个答案:

答案 0 :(得分:0)

错误由odataapplication/json媒体类型的Accept参数触发。我相信odata.metadata参数是您真正想要的:

Accept: application/json;odata.metadata=verbose

<强>原始

错误消息肯定有点奇怪,但我会猜测并说你的根本原因很可能与错误的URL有关。您提供的是driveId,但您正在访问drive单例,而不是drives集合。试试这个,看看它是否有帮助:

https://myonedrive/_api/v2.0/drives/{driveId}/items/{parentfolderId}/children/{fileName}/content