Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created May 4, 2018 22:54
Show Gist options
  • Save senthilmpro/537e5af8dd0d3455c9f8718f03efa1a6 to your computer and use it in GitHub Desktop.
Save senthilmpro/537e5af8dd0d3455c9f8718f03efa1a6 to your computer and use it in GitHub Desktop.
Download Images to Disk using Axios JS
// 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