admin管理员组文章数量:1336632
I recently asked/accepted an answer to a question I had earlier: How can I replicate the functionality of a wget with nodejs.
Now, my script is functioning perfectly, but I'd like to be able to show the user the percentage that's downloaded. I'm not sure if that's exposed to us (I didn't see it in the docs), but I figured I'd ask here anyways. Would love some help!
Thanks!
I recently asked/accepted an answer to a question I had earlier: How can I replicate the functionality of a wget with nodejs.
Now, my script is functioning perfectly, but I'd like to be able to show the user the percentage that's downloaded. I'm not sure if that's exposed to us (I didn't see it in the docs), but I figured I'd ask here anyways. Would love some help!
Thanks!
Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Mar 2, 2012 at 23:00 ConnorConnor 4,1688 gold badges37 silver badges52 bronze badges1 Answer
Reset to default 8Yes, you can. With child_process.spawn
. While child_process.exec
executes the mand and buffers the output, spawn
gives you events on data
, error
and end
. So you can listen to that and calculate your progress. There's a basic example in the node docs for spawn.
Update: I saw your other question. You can use wget
for this, but I remend the nodejs module request instead. Here's how to fetch the file with request:
var request = require("request");
request(url, function(err, res, body) {
// Do funky stuff with body
});
If you want to track progress, you pass a callback to 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
});
本文标签: javascriptShow progress on nodejs childprocessexecStack Overflow
版权声明:本文标题:javascript - Show progress on node.js child_process.exec? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742414192a2470388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论