【发布时间】:2019-08-05 20:55:39
【问题描述】:
我正在尝试从 url 获取 zip 文件,以便在下一步将其上传到 Google 驱动器。但是我的代码不起作用。
// The method to get the zip File from the url
function getFile(){
var file = request({
method : "GET",
url : "https://start.spring.io/starter.zip",
encoding: null // <- this one is important !
}, function (error, response, body) {
if(error || response.statusCode !== 200) {
// handle error
return;
}
JSZip.loadAsync(body).then(function (zip) {
return zip.file("content.txt").async("string");
}).then(function () {
console.log(text);
});
});
}
// method to upload zip to drive
function uploadFile(auth) {
const drive = google.drive({ version: 'v3', auth });
var fileMetadata = {
'name': 'demo.zip'
};
var media = {
mimeType: 'application/zip',
body: fs.createReadStream(getFile())
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, res) {
if (err) {
// Handle error
console.log(err);
} else {
console.log('File Id: ', res.data.id);
}
});
}
我想从上面的 url 中获取 zip 文件,但是它抛出了一个异常:
new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
当我将正文:fs.createReadStream(getFile()) 更改为正文:fs.createReadStream("https://start.spring.io/starter.zip")
the Exception is: no such file or directory, open 'https://start.spring.io/starter.zip
【问题讨论】:
标签: node.js url request google-drive-api