我正在尝试使用Microsoft Graph API创建word文件。 link
以下是相同的代码。
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桌面应用程序中打开。
它出现以下错误。
答案 0 :(得分:0)
Microsoft Graph API没有创建文件的方法,因此我们使用文件上载方法,但是这种情况下必须为二进制。只需删除data属性,它将可以正常工作。
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);
});
}
token = `eyJ0eXAiOiJKV1QiLCJub25jZSI6ImFTek05SzJiU1JyWllubjhaUlYyOFpESGp6bmxtQno1Y1VmVjNHMVYxeXMiLCJhbGciOiJSUzI1NiIsIng1dCI6IkN0VHVoTUptRDVNN0RMZHpEMnYyeDNRS1NSWSIsImtpZCI6IkN0VHVoTUptRDVNN0RMZHpEMnYyeDNRS1NSWSJ9.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTAwMDAtYzAwMC0wMDAwMDAwMDAwMDAiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC9kMzA4NjIwOC1jOGQxLTQyMTgtOGI1NS1jYWRjNzJiMDFlMDUvIiwiaWF0IjoxNTkwMDAwNDc4LCJuYmYiOjE1OTAwMDA0NzgsImV4cCI6MTU5MDAwNDM3OCwiYWNjdCI6MCwiYWNyIjoiMSIsImFpbyI6IkFTUUEyLzhQQUFBQXNnQ1VzK3JCMElFb2xQMmt4SUNJSWdKaXhYM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fixgDY4Ykf2E_dGPKdHPBtu0SScu_KT4zSSfZubs4IYu8bZL0k8c19Sv6DffC9IPayGv7un4cmObG6sUw5_N28hXtvg-HImcrwkep5gt5xmRP2yqpdL_XZ2DjTGBh_rc6ZnFIUMptl7_6s07arD9Rlheyn7faERR_94bSl4hiEGTrUlPCDuTxpo9BfWMqyAeOec4aoviCJvZD5ySfTCIRbTuyXRPndwBwNIuQX3lQRpMB1I60pGO-7gYX04vslpFwIXWQ1x2CyPsKjeLjw`
createWordFile(token)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
答案 1 :(得分:0)
发现诀窍是在我的项目中托管一个 .xlsx 或 .pptx 空白文件。当我需要从 javascript 在 OneDrive 中创建一个新文件时,我获取如下文档:
String[][] emp = {
{"Victor ", "ZSA"},
{"Fred ", "HAN"},
{"Drake ", "SOL"},
{"Albert ", "TUR"},
{"Eric ", "CAN"}};
// sort by first column alphabetically
Arrays.sort(emp, Comparator.comparing(arr -> arr[0]));
// output
Arrays.stream(emp).map(Arrays::toString).forEach(System.out::println);
一个我有我的文件。我现在可以使用 Microsoft Graph Client Javascript SDK 创建一个文档,如下所示:
[Albert , TUR]
[Drake , SOL]
[Eric , CAN]
[Fred , HAN]
[Victor , ZSA]