【发布时间】:2018-10-16 10:45:06
【问题描述】:
将图片 URL 转换为 base64 字符串后,我尝试将此 base64 图片上传到 Goolge Drive,一切正常:响应代码 200 和 Goole Drive 在 App/Web 中显示此文件。
但是当我打开这张图片时,它就像谷歌在base64字符串上改变了一些东西,所以它无法读取。
下载图像从 GD 转换为 base64 字符串后,我看到我原来的 base64 字符串发生了变化。
上传前:https://anotepad.com/notes/nt6fct
来自 Google 云端硬盘的文件:https://anotepad.com/notes/ayg57q
上传代码:
testUploadFile(fileInfo, base64Data){
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var contentType = fileInfo.type || 'application/octet-stream';
var metadata = {
'name': fileInfo.title,
'mimeType': contentType
};
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;
gapi.client.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': {
'uploadType': 'multipart'
},
'headers': {
'Content-Type': 'multipart/related; boundary="' + boundary + '"'
},
'body': multipartRequestBody
}).then(function(response){
console.log(response);
});
}
出了点问题@@你能纠正一下吗? 提前致谢!
【问题讨论】:
标签: javascript google-api google-drive-api base64 google-api-js-client