【问题标题】:Upload Base64 image to Google drive using javascript使用 javascript 将 Base64 图像上传到 Google 驱动器
【发布时间】:2014-05-26 07:32:01
【问题描述】:

我正在尝试使用 Jquery AJAX POST 请求将 Base64 编码图像上传到谷歌驱动器。 IT 将数据上传到 Google 云端硬盘,但在下载文件后未在 Google 云端硬盘查看器上显示图像。

请求调用获得带有 JSON 响应的成功消息。

以下是代码的sn-p:[imageData is Base64 encoding string]

$.ajax({
        type: 'POST',
        url: 'https://www.googleapis.com/upload/drive/v2/files?uploadType=media',
        headers: {
          'Authorization': 'Bearer ' + access_token,
          'Content-Type': 'image/jpeg',
          'Content-Length': imageData.length, // imageData is Base64 encoded string
        },
        data: imageData // imageData is Base64 encoded string
       }).done(function (result, textStatus, jqXHR) {
                console.log('success : ' + textStatus);
                console.log(JSON.stringify(result));

            }).fail(function (jqXHR, textStatus, errorThrown) {
                console.log('fail : ' + textStatus + ' desc : ' + SON.stringify(jqXHR));
            });

提前致谢。

  • Vishrut

【问题讨论】:

标签: jquery ajax image upload base64


【解决方案1】:

这会上传数据,但不会上传在云端硬盘中显示图片所需的数据。通过上传 base64 格式,您将一个大字符串而不是二进制图像文件上传到 Google Drive。 base64 编码包含与原始文件不同的字节,并且在驱动器中生成的文件比相关二进制文件大约 50%。

如果你下载base64文件,把它转成图片数据url显示,就可以正常显示了。但是您将无法在 google drive web UI 中看到它,因为它显然按原样存储 base64 数据,而不是将其转换为二进制文件。 (截至 2017 年 11 月)

gapi 不能很好地处理 blob 上传,因此不妨尝试使用 googleapi's cors-upload-sample from githubmy fork as described here

【讨论】:

    猜你喜欢
    • 2012-11-02
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多