Created
June 19, 2018 10:53
-
-
Save lockevn/aa6a78dceab9cd4153e36600dff5ac9b to your computer and use it in GitHub Desktop.
Download image (and binary file) to a file on disk, using axios
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
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