我试图从一个URL获取zip文件,以便在下一步上传到Google Drive。但是我的代码不起作用。
// 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("https://start.spring.io/starter.zip")
,而不是fs.createReadStream(getFile())
时。the Exception is: no such file or directory, open 'https://start.spring.io/starter.zip