Skip to content

Instantly share code, notes, and snippets.

@lockevn
Created June 19, 2018 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lockevn/aa6a78dceab9cd4153e36600dff5ac9b to your computer and use it in GitHub Desktop.
Save lockevn/aa6a78dceab9cd4153e36600dff5ac9b to your computer and use it in GitHub Desktop.
Download image (and binary file) to a file on disk, using axios
const axios = require('axios')
/**
* Download url to a file on disk
* @param url
* @param destFile
*/
async function downloadAxios(url: string, destFile: string) {
const response = await axios({
method: 'GET',
url: url,
responseType: 'stream' // axios image download
})
response.data.pipe(fs.createWriteStream(destFile)) // pipe the result stream directly to a file
// return a promise
return new Promise((resolve, reject) => {
response.data.on('end', () => {
resolve()
})
response.data.on('error', () => {
reject()
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment