通过请求获取Node.js中的下载进度 [英] Get download progress in Node.js with request

查看:510
本文介绍了通过请求获取Node.js中的下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用Node模块请求下载应用程序文件的更新程序。如何使用 chunk.length 来估计剩余的文件大小?这是我代码的一部分:

  var file_url ='http://foo.com/bar.zip'; 
var out = fs.createWriteStream('baz.zip');

var req = request({
method:'GET',
uri:file_url
});

req.pipe(out);

req.on('data',function(chunk){
console.log(chunk.length);
});

req.on('end',function(){
// do something
});


解决方案

这应该让你想要的总数: p>

  req.on('response',function(data){
console.log(data.headers ['content- length']);
});

我的内容长度为 9404541 / p>

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 size? Here's part of my code:

var file_url = 'http://foo.com/bar.zip';
var out = fs.createWriteStream('baz.zip');

var req = request({
    method: 'GET',
    uri: file_url
});

req.pipe(out);

req.on('data', function (chunk) {
    console.log(chunk.length);
});

req.on('end', function() {
    //Do something
});

解决方案

This should get you the total you want:

req.on( 'response', function ( data ) {
    console.log( data.headers[ 'content-length' ] );
} );

I get a content length of 9404541

这篇关于通过请求获取Node.js中的下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆