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

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

问题描述

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

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' ] );
} );

我得到的内容长度为 9404541

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

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