Office365 使用Microsoft Graph api创建Word文件
我正在尝试使用Microsoft Graph API创建word文件。 下面是相同的代码Office365 使用Microsoft Graph api创建Word文件,office365,microsoft-graph-api,onedrive,office365api,Office365,Microsoft Graph Api,Onedrive,Office365api,我正在尝试使用Microsoft Graph API创建word文件。 下面是相同的代码 function createWordFile(token) { $.ajax({ url: 'https://graph.microsoft.com/v1.0/me/drive/root:/FolderA/FileU.docx:/content', method: 'PUT', headers: { 'authorizati
function createWordFile(token) {
$.ajax({
url: 'https://graph.microsoft.com/v1.0/me/drive/root:/FolderA/FileU.docx:/content',
method: 'PUT',
headers: {
'authorization': 'Bearer ' + token,
"content-type": "text/plain",
'Accept': 'application/json;odata.metadata=full'
},
data: 'test',
transformRequest: []
}).success(function (result) {
console.log(result);
}).error(function (err) {
console.log(err);
});
}
已成功创建文件。但该文件已损坏,无法在Word Online或Word Desktop App中打开
它给出了以下错误
Microsoft Graph API没有创建文件的方法,因此我们使用文件上载方法,但这种情况必须是二进制的。只需删除数据属性,它就能完美工作
函数createWordFile(令牌){
$.ajax({
网址:'https://graph.microsoft.com/v1.0/me/drive/root:/FolderA/FileU.docx:/content',
方法:'放',
标题:{
“授权”:“持票人”+代币,
“内容类型”:“文本/普通”,
“接受”:“应用程序/json;odata.metadata=full”
},
数据:'测试',
请求:[]
}).成功(功能(结果){
控制台日志(结果);
}).error(函数(err){
控制台日志(err);
});
}
令牌=`EYJ0EXIIJKv25JZSI6IMFTEK05SZUJU1JU1JYW1VJN6MXTQNO1VJNHMVYXEXMILJH6HbGcIJJ0V0V0V0V0RmNYYEDN1N1NSISIMTpZCI6IKN0V0R0R0RmNZZZH0RJJ0R0R0RJU1V0RJJU1V0V0JJU1V0JU1V0JJU2J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0J0IKFTUEYLZHQQFBQNQ1VZK3JCMELFB2xQMMT4 SUNJWDKAXHYLY1JYRFM3BXVHOHJJDN9IYJPBINB3ZCHBFZGLZGZJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJTw9Saw5HzGryJoinZpy3RvCiSiSiM9PzCiNizMnZWmTbLmDuttingQZNs05YZg1LtG4Zy5YTa5MyIsinSyxRmiJoin1AzMjawMejFnzyMTI2IwZyZyZyZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZiZi2.B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B ZX32 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 9rlheyn7faerru94bsl4hiegtruLPCDUTXPO9BFWMQYAEOEC4AOVICJVZD5YSFTCIRBTUYXRPNDWNIUQX3LQRPMB1I60PGO-7gYX04vslpFwIXWQ1x2CyPsKjeLjw`
createWordFile(令牌)
发现的诀窍是在我的项目中托管一个.xlsx或.pptx空白文件。当我需要从javascript在OneDrive中创建一个新文件时,我会获取如下文档:
return (fetch('/assets/file-samples/template.xlsx')
.then((res) => {
return res.arrayBuffer();
})
.then((buf) => {
return new File([buf], 'template.xlsx', {type:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
})
);
this.getSampleFile(fileType, extension).then((blankFile) => {
client.api('me/drive/root:/yourfolder/' + 'yourName.xlsx' + ':/content')
.put(blankFile).then((file) => {
console.log(file)
}, (err) => {
console.log(err)
});
});
我有我的档案。我现在可以使用Microsoft Graph Client Javascript SDK创建文档,如下所示:
return (fetch('/assets/file-samples/template.xlsx')
.then((res) => {
return res.arrayBuffer();
})
.then((buf) => {
return new File([buf], 'template.xlsx', {type:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
})
);
this.getSampleFile(fileType, extension).then((blankFile) => {
client.api('me/drive/root:/yourfolder/' + 'yourName.xlsx' + ':/content')
.put(blankFile).then((file) => {
console.log(file)
}, (err) => {
console.log(err)
});
});
本地副本和上载副本的文件大小是否相同?没有创建的文件大小接近100 kb。虽然本地副本是2.5MB,但查看示例代码时,我看不到您将文件传递到哪里<代码>数据:'test'不正确,因为负载应该是二进制文件,而不是字符串。在这种情况下,如何将字符串转换为二进制文件?我不确定我是否遵循。您正在上载word文档,因此负载应该是实际文档(二进制),而不是字符串。