Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dimawebmaker/4e46da94ee59f93d687e86bb582c3891 to your computer and use it in GitHub Desktop.
Save dimawebmaker/4e46da94ee59f93d687e86bb582c3891 to your computer and use it in GitHub Desktop.
Download File using axios : Node.js program
'use strict'
const Fs = require('fs')
const Path = require('path')
const Axios = require('axios')
async function downloadImage () {
const url = 'https://unsplash.com/photos/AaEQmoufHLk/download?force=true'
const path = Path.resolve(__dirname, 'images', 'code1.jpg')
// axios image download with response type "stream"
const response = await Axios({
method: 'GET',
url: url,
responseType: 'stream'
})
// pipe the result stream into a file on disc
response.data.pipe(Fs.createWriteStream(path))
// return a promise and resolve when download finishes
return new Promise((resolve, reject) => {
response.data.on('end', () => {
resolve()
})
response.data.on('error', () => {
reject()
})
})
}
async function Main(){
const data = await downloadImage();
console.log("DATA ", data);
}
Main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment