如何计算进度条中的恢复和暂停数据量

时间:2019-02-14 05:42:19

标签: javascript node.js request

暂停和恢复功能后如何计算接收到的数据。

我正在进行文件下载,包括下载暂停和恢复。

它可以暂停,但是当我恢复它时,它不会达到100%。它会在100%之前停止

  function download_file(event, filename) {
          req2 = request({
            method: 'GET',
            uri: fileURL
          });
            var out = fs.createWriteStream(finalPath);
            req2.pipe(out);
            req2.on('response', function(data) {
total_bytes = parseInt(data.headers['content-length']);
    progressBar.max = total_bytes;
    total_size = total_bytes;
            });
            req2.on('data', function(chunk) {
                    received_bytes += chunk.length;
 console.log(received_bytes);
progressBar.value = received_bytes;
display.innerText = Math.round((progressBar.value / progressBar.max) * 1000) / 10 + '%'
              }
            });
        };

    function pause22() {
      console.log('pause function called');
      req2.pause();
    }

        function resume22() {
          console.log('RESUME function called');
 progressBar.max=progressBar.max-progressBar.value;
  console.log(progressBar.max);
  console.log(progressBar.value);
   progressBar.value = received_bytes;
   received_bytes= progressBar.value;
  req2.resume();
        }

1 个答案:

答案 0 :(得分:1)

假设您要继续下载时停止的地方,并且将“ total_bytes”和“ received_bytes”存储为全局变量,那么进度条将简单地重新建立到这些位置。您的简历22应该是...

function resume22() {
      console.log('RESUME function called');
      progressBar.max=total_bytes;
      console.log(progressBar.max);
      progressBar.value = received_bytes;
      console.log(progressBar.value);
      req2.resume();
    }