【问题标题】:Uploading the contents of an ArrayBuffer to Google Drive using Google Drive API v3 and Javascript使用 Google Drive API v3 和 Javascript 将 ArrayBuffer 的内容上传到 Google Drive
【发布时间】:2016-11-04 12:42:46
【问题描述】:

我正在尝试在 Google Drive 上创建一个包含 ArrayBuffer 内容的文件。我正在使用 Javascript 和 Drive API 的第 3 版。

我设法使用this code sample 创建了一个填充文本文件,但我找不到任何关于如何使用 ArrayBuffer 中的二进制数据填充文件的示例。这是可能的,还是我必须在将二进制数据上传到 Google Drive 之前将其编码为字符串?

Google 的 v3 文档确实缺乏:-(

非常感谢任何帮助。

【问题讨论】:

  • 这可能会有所帮助:github.com/googledrive/web-quickeditor/blob/master/src/… 使用 V3 上传文件不应该与 v2 不同。我同意 V3 还没有太多文档。
  • @DaImTo 感谢您的链接。您能否指出正确的方向,如何使用此代码使用 ArrayBuffer 中的数据更新文件?

标签: google-drive-api


【解决方案1】:

我也为此苦苦挣扎,并且能够通过对 ArrayBuffer 的基于 UTF-8 的字符串表示进行 base64 处理来使其工作。它看起来像这样:

// file.data is an ArrayBuffer
var b64str = btoa(String.fromCharCode.apply(null, new Uint8Array(file.data)));

然后您需要在多部分文件上传中指定 Content-Transfer-Encoding 以便 Drive api 理解文件:

// 'data' is our base64'd image that came from ArrayBuffer
var requestBody =
    '--' + boundary_string + '\n' +
    "Content-Type: application/json; charset=UTF-8\n\n" +
    JSON.stringify(metadata) + '\n' +
    '--' + boundary_string + '\n' +
    "Content-Type: " + mime + "\n" +
    "Content-Transfer-Encoding: base64" +
    data + '\n' +
    '--' + boundary_string + '--';

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多