23 lines
680 B
JavaScript
23 lines
680 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')
|