是的你可以。和child_process.spawn
. While child_process.exec
执行命令并缓冲输出,spawn
为您提供事件data
, error
and end
。所以你可以听一下并计算你的进度。有一个基本的例子生成的节点文档 http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_spawn_command_args_options.
Update:我看到了你的另一个问题。您可以使用wget
为此,但我推荐使用nodejs模块request https://github.com/mikeal/request反而。以下是如何通过请求获取文件:
var request = require("request");
request(url, function(err, res, body) {
// Do funky stuff with body
});
如果您想跟踪进度,您可以将回调传递给onResponse
:
function trackProgress(err, res) {
if(err)
return console.error(err);
var contentLength = parseInt(res.headers["content-length"], 10),
received = 0, progress = 0;
res.on("data", function(data) {
received += data.length;
progress = received / contentLength;
// Do funky stuff with progress
});
}
request({url: url, onResponse: trackProgress}, function(err, res, body) {
// Do funky stuff with body
});