core/bin/download.js

23 lines
680 B
JavaScript
Raw Normal View History

2019-12-17 20:55:50 +08:00
const fs = require('fs')
const path = require('path')
const Axios = require('axios')
async function downloadFile(url, filepath, name) {
if (!fs.existsSync(filepath)) {
fs.mkdirSync(filepath);
}
const mypath = path.resolve(filepath, name);
const writer = fs.createWriteStream(mypath);
const response = await Axios({
url,
method: "GET",
responseType: "stream",
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
// downloadFile("https://www.cnblogs.com/images/logo_small.gif",__dirname + "/dow",'asd.gif')