我正在尝试使用 Jquery AJAX POST 请求将 Base64 编码的图像上传到 Google Drive。 IT 人员将数据上传到 Google 云端硬盘,但下载文件后,它并未在 Google 云端硬盘查看器上显示图像。
请求调用获取带有 JSON 响应的成功消息。
以下是代码片段:[imageData 是 Base64 编码的字符串]
$.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));
});
提前致谢。
- 维什鲁特
最佳答案
这会上传数据,但不会上传在云端硬盘中显示图片所需的数据。通过上传 base64 格式,您将一个大字符串而不是二进制图像文件上传到 Google Drive。 Base64 编码包含与原始文件不同的字节,并且在驱动器中生成的文件比关联的二进制文件大大约 50%。
如果你下载base64文件,并将其转为图像数据url来显示,它会显示正常。但您将无法在 google Drive Web UI 中看到它,因为它显然按原样存储 base64 数据,而不是将其转换为二进制文件。 (截至2017年11月)
gapi
不能很好地处理 blob 上传,因此也许可以尝试使用 googleapi's cors-upload-sample from github或my fork as described here
关于jquery - 使用 javascript 将 Base64 图像上传到 Google 云端硬盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024532/