Last active
June 21, 2017 23:57
-
-
Save EsauPR/af77c9a9e29c9197828b98cd6bfa1dee to your computer and use it in GitHub Desktop.
Download file from 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
const http = require('http'); | |
function downloadFile(url) { | |
return new Promise((resolve, reject) => { | |
const chunks = []; | |
const request = http.get(url, response => { | |
response.on('data', chunk => { | |
chunks.push(chunk); | |
}); | |
response.on('end', () => { | |
const file = new Buffer.concat(chunks).toString('base64'); | |
resolve(file); | |
}); | |
}); | |
request.on('error', (error) => { | |
logger.error(error.message); | |
reject('Can\'t download file'); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment