kevva / download

Download and extract files
MIT License
1.28k stars 199 forks source link

Progress bar and current % of download (useful for big files) #227

Open jfoclpf opened 2 years ago

jfoclpf commented 2 years ago

Any tips on how to use progress bar or % for the download (very useful for big files)?

jfoclpf commented 2 years ago

Created PR https://github.com/kevva/download/pull/228 which addresses the current issue

jfoclpf commented 2 years ago

For big files, it may be useful to use a progress bar. You must install also module progress.

const download = require('download')
const ProgressBar = require('progress')

const writeStream = fs.createWriteStream('http://unicorn.com/foo.zip')
const readStream = download('dist/foo.zip')
readStream.on('response', function (res) {
  const len = parseInt(res.headers['content-length'], 10)
  const bar = new ProgressBar('  downloading [:bar] :rate/bps :percent :etas', {
    complete: '=',
    incomplete: ' ',
    width: 20,
    total: len
  })
  readStream.on('data', function (chunk) {
    writeStream.write(chunk)
    bar.tick(chunk.length)
  })
  readStream.on('end', function () {
    console.log('Download done with success\n')
    writeStream.end()
  })
})

image

codad5 commented 1 year ago

For big files, it may be useful to use a progress bar. You must install also module progress.

const download = require('download')
const ProgressBar = require('progress')

const writeStream = fs.createWriteStream('http://unicorn.com/foo.zip')
const readStream = download('dist/foo.zip')
readStream.on('response', function (res) {
  const len = parseInt(res.headers['content-length'], 10)
  const bar = new ProgressBar('  downloading [:bar] :rate/bps :percent :etas', {
    complete: '=',
    incomplete: ' ',
    width: 20,
    total: len
  })
  readStream.on('data', function (chunk) {
    writeStream.write(chunk)
    bar.tick(chunk.length)
  })
  readStream.on('end', function () {
    console.log('Download done with success\n')
    writeStream.end()
  })
})

image

Isn't the create write stream to be the download path while the first parameter in the download function is the URL?

jfoclpf commented 1 year ago

No, it might seem strange but I am almost sure the code is correct, I tested myself.

codad5 commented 1 year ago

I had to switch it to work fine tho .