[英]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. 我在成功呼叫中遇到错误,内容没有上传到文件中。
The error is being triggered by the odata
parameter for the application/json
media type in the Accept
. 该错误是由
Accept
的application/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.