Javascript 成功上传到Google Drive,但打开时文件为空(仍有大小)
将图像URL转换为base64字符串后,我尝试将此base64图像上载到Goolge Drive,一切正常:响应代码200和Goole Drive在App/Web中显示此文件 但当我打开这张图片时,它就像谷歌在base64字符串上更改了一些东西,所以它无法读取 在下载并将图像从GD转换为base64字符串之后,我看到原来的base64字符串被更改了 上传前: 来自Google Drive的文件: 上传代码:Javascript 成功上传到Google Drive,但打开时文件为空(仍有大小),javascript,google-api,google-drive-api,base64,google-api-js-client,Javascript,Google Api,Google Drive Api,Base64,Google Api Js Client,将图像URL转换为base64字符串后,我尝试将此base64图像上载到Goolge Drive,一切正常:响应代码200和Goole Drive在App/Web中显示此文件 但当我打开这张图片时,它就像谷歌在base64字符串上更改了一些东西,所以它无法读取 在下载并将图像从GD转换为base64字符串之后,我看到原来的base64字符串被更改了 上传前: 来自Google Drive的文件: 上传代码: testUploadFile(fileInfo, base64Data){ co
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);
});
}
出了点问题@你能纠正一下吗?
提前谢谢 这个简单的示例可能会帮助您找到代码中的问题
function createFile(){
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var fileContent = 'It works :)';
var metadata = {
'name': 'myFile',
'mimeType': 'text/plain\r\n\r\n'
};
var multipartRequestBody = delimiter + 'Content-Type: application/json\r\n\r\n' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + 'text/plain\r\n\r\n' + fileContent + 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);
});
}
谢谢DalmTo:D我忘了+'\r\n\r\n'。