Last active
November 25, 2019 13:59
-
-
Save adityathebe/bd7c11d83732d9311361d1402a4dff87 to your computer and use it in GitHub Desktop.
Download files with axios.js in Nodejs
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
/** | |
* @param {String} uri url of the file to download | |
* @param {String} filePath File Path | |
*/ | |
function _downloadFile(uri, filePath) { | |
return new Promise((resolve, reject) => { | |
axios({ | |
method: 'get', | |
url: uri, | |
responseType: 'stream', | |
}).then(response => { | |
const stream = response.data; | |
stream | |
.pipe(fs.createWriteStream(filePath)) | |
.on('finish', resolve) | |
.on('error', reject); | |
}).catch(reject); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment