Get download progress in Node.js with request

前端 未结 6 2233
清酒与你
清酒与你 2020-12-02 15:27

I\'m creating an updater that downloads application files using the Node module request. How can I use chunk.length to estimate the remaining file

6条回答
  •  天命终不由人
    2020-12-02 16:30

    Using the cool node-request-progress module, you could do something like this in es2015:

    import { createWriteStream } from 'fs'
    import request from 'request'
    import progress from 'request-progress'
    
    progress(request('http://foo.com/bar.zip'))
     .on('progress', state => {
    
       console.log(state)
    
       /*
       {
           percentage: 0.5,        // Overall percentage (between 0 to 1)
           speed: 554732,          // The download speed in bytes/sec
           size: {
             total: 90044871,      // The total payload size in bytes
             transferred: 27610959 // The transferred payload size in bytes
           },
           time: {
             elapsed: 36.235,      // The total elapsed seconds since the start (3 decimals)
             remaining: 81.403     // The remaining seconds to finish (3 decimals)
           }
       }
       */
    
      })
      .on('error', err => console.log(err))
      .on('end', () => {})
      .pipe(createWriteStream('bar.zip'))
    

提交回复
热议问题