core/bin/download.js
2019-12-26 17:37:51 +08:00

23 lines
731 B
JavaScript

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')
module.exports = downloadFile