Created
April 19, 2019 20:43
-
-
Save Saccarab/c547f1b1c04512ba743f96e980891e48 to your computer and use it in GitHub Desktop.
https get file download with progress percentage
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 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