Created
May 4, 2018 22:54
-
-
Save senthilmpro/537e5af8dd0d3455c9f8718f03efa1a6 to your computer and use it in GitHub Desktop.
Download Images to Disk using Axios JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sample code to DL using axios. | |
const axios = require("axios"), | |
fs = require("fs"), | |
path = require("path"); | |
const SUB_FOLDER = ""; | |
const IMG_NAME = "img.jpg"; | |
/** | |
* this will dl.image | |
* @param {*} reqUrl | |
*/ | |
function dlImage(reqUrl) { | |
const dir = path.resolve(__dirname, SUB_FOLDER, IMG_NAME); | |
axios({ | |
method: "GET", | |
url: reqUrl, | |
responseType: "stream" | |
}).then(res => { | |
res.data.pipe(fs.createWriteStream(dir)); | |
res.data.on("end", () => { | |
console.log("download complete"); | |
}); | |
}); | |
} | |
const reqUrl = | |
"https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-644280.jpg"; | |
dlImage(reqUrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment