【发布时间】:2016-07-22 00:44:31
【问题描述】:
我一直在使用 JavaScript Google Drive API 来创建(插入)一个新文件。它很好用,只是它创建的文档不是 google doc,而是某种可以转换为 google doc 的文档。
我想要一份真正的谷歌文档。我尝试了“convert:true”,但它似乎没有将其转换为文档。
insertFile: function (filename, content, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
{
var contentType = 'application/vnd.google-apps.document';
var metadata = {
'title': filename,
'mimeType': 'text/html',
'description': 'Created'
};
// TODO: replace this with a library - https://github.com/beatgammit/base64-js/blob/master/lib/b64.js
var base64Data = window.btoa(unescape(encodeURIComponent(content)));
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart', 'convert': true },
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
if (!callback) {
callback = function (file) {
console.log(file)
};
}
request.execute(callback);
}
}
插入文件后,当我转到链接时,我得到:
【问题讨论】:
标签: javascript google-drive-api