Reputation: 1
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.
Upvotes: 0
Views: 219
Reputation: 4202
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
Upvotes: 0