简体   繁体   English

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

[英]Unable To Put/Upload content to a file in OneDrive For Business using OneDrive for Business API

I am trying to put/add content to file in OneDrive for Business using OneDrive for Business REST API. 我正在尝试使用OneDrive for Business REST API将内容添加/添加到OneDrive for Business中的文件中。 Below is the code snippet: 下面是代码片段:

 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();
}

I am getting below error in the success call and content is not getting uploaded in the file. 我在成功呼叫中遇到错误,内容没有上传到文件中。

ErrorMessage 错误信息

The error is being triggered by the odata parameter for the application/json media type in the Accept . 该错误是由Acceptapplication/json媒体类型的odata参数触发的。 I believe the odata.metadata parameter is what you actually want: 我相信odata.metadata参数是您真正想要的:

Accept: application/json;odata.metadata=verbose

Original 原版的

The error message is definitely a little strange, but I'm going to take a guess and say your root cause is most likely related to a bad URL. 错误消息肯定有点奇怪,但是我要猜​​测一下,说您的根本原因很可能与URL错误有关。 You're providing a driveId but you're accessing the drive singleton and not the drives collection. 您提供的是driveId但您正在访问drive单例而不是drives集合。 Try this and see if it helps: 试试看,看看是否有帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM