Skip to content

Instantly share code, notes, and snippets.

@Saccarab
Created April 19, 2019 20:43
Show Gist options
  • Save Saccarab/c547f1b1c04512ba743f96e980891e48 to your computer and use it in GitHub Desktop.
Save Saccarab/c547f1b1c04512ba743f96e980891e48 to your computer and use it in GitHub Desktop.
https get file download with progress percentage
const request = https.get(url, (response) => {
if (response.statusCode === 200) {
var len = parseInt(response.headers['content-length'], 10);
var downloaded = 0
response.pipe(fs.createWriteStream(filepath)
.on("error", (error) => {
//error
})
.on('finish', () => {
//finish
})
)
response.on('data', (chunk) => {
downloaded += chunk.length;
console.log(`${(100 * downloaded / len).toFixed(2)} %`)
})
response.on('error', (error) => {
//error
})
}
else {
//bad response code
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment