下载后,我尝试从URL上传文件,但是无法正常工作。我曾尝试在Google上进行搜索,并找到了实现此目的的方法,例如DriveApp(我找不到如何导入DriveApp类,我是javascript新手。)
那么,还有其他方法可以做到吗?我已成功将文件上传到Google云端硬盘,但是Google云端硬盘无法读取内容。我认为下载文件后我做错了,请您帮我改正。
提前谢谢!
从URL下载:
var _this_ = this;
var xhr = new XMLHttpRequest();
xhr.open('GET', this.link, true);
xhr.responseType='blob';
xhr.onload = function(e) {
if (xhr.status == 200)
{
_this_.uploadToGoogleDrive(xhr.response);
}
};
xhr.send();
并上传到Google云端硬盘:
uploadFile(title,data) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
let fileData = new Blob([data], {
'type': this.media.getMimeType(title)
});
//const contentType = 'application/json';
const contentType = fileData.type || 'application/octet-stream';
var metadata = {
'name': title,
'mimeType': contentType
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n\r\n' +
fileData +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/related; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
request.execute();
}