简体   繁体   中英

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. 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 . I believe the odata.metadata parameter is what you actually want:

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. You're providing a driveId but you're accessing the drive singleton and not the drives collection. Try this and see if it helps:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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