download icon indicating copy to clipboard operation
download copied to clipboard

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

Open jfoclpf opened this issue 3 years ago • 5 comments

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

jfoclpf avatar Aug 20 '22 13:08 jfoclpf

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

jfoclpf avatar Aug 20 '22 14:08 jfoclpf

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

jfoclpf avatar Aug 22 '22 13:08 jfoclpf

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?

codad5 avatar Feb 06 '23 04:02 codad5

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

jfoclpf avatar Feb 06 '23 08:02 jfoclpf

I had to switch it to work fine tho .

codad5 avatar Feb 19 '23 15:02 codad5