我正在尝试使用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();
}
我在成功通话中收到以下错误,内容未上传到文件中。
答案 0 :(得分:0)
错误由odata
中application/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