【问题标题】:Google Drive API - JavaScript Insert File - Create native google docGoogle Drive API - JavaScript 插入文件 - 创建原生谷歌文档
【发布时间】: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


    【解决方案1】:

    我想我发现了你的错误。尝试迁移到v3,根据v3 migration

    其他变化

    现在通过在资源正文中设置适当的目标 mimeType 来请求导入到 Google Docs 格式,而不是指定 ?convert=true。

    如果格式不受支持,导入操作将返回 400 错误。

    这是list of supported mimeType的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多