通过nodejs spawn执行wget时动态进度条无法正确显示
我编写了一个node.js 代码,就像下面的示例一样。
我使用 child_process.spawn 创建子进程来执行命令“wget”。同时我将子进程的标准 I/O 重定向到 Node.js 进程的标准 I/O
let cp = spawn('wget', 'some_file_url')
cp.stdin.pipe(process.stdin)
cp.stdout.pipe(process.stdout)
cp.stderr.pipe(process.stderr)
现在我在 Node.js 中执行上面的代码,一切正常,但进度条。它看起来像这样:
0K .......... .......... .......... .......... .......... 0% 134K 5m21s
50K .......... .......... .......... .......... .......... 0% 631K 3m14s
100K .......... .......... .......... .......... .......... 0% 4.64M 2m12s
150K .......... .......... .......... .......... .......... 0% 167M 99s
通常,如果我在终端上执行 wget 命令,
$ wget some_file_url
动态进度条将显示在输出中
file.tar.gz 15%[======> ] 6.69M 2.27MB/s
,所以,我应该如何使进度条显示如上面的
环境
- node.js: v14.16.1
- wget: GNU Wget 1.21 .2 建立在 darwin21.1.0 上,
抱歉
我的英语很差。请原谅语法或打字错误。我正在尽力改进。
i write a node.js code just like below sample.
i use child_process.spawn to create child process for execute command 'wget'. at the same time i redirec standard i/o of child process to standard i/o of node.js process
let cp = spawn('wget', 'some_file_url')
cp.stdin.pipe(process.stdin)
cp.stdout.pipe(process.stdout)
cp.stderr.pipe(process.stderr)
now i execute code above in node.js, everything is ok but progress bar. it just looks like this:
0K .......... .......... .......... .......... .......... 0% 134K 5m21s
50K .......... .......... .......... .......... .......... 0% 631K 3m14s
100K .......... .......... .......... .......... .......... 0% 4.64M 2m12s
150K .......... .......... .......... .......... .......... 0% 167M 99s
normally, if i execute wget command on terminal
$ wget some_file_url
a dynamic progress bar will display in output
file.tar.gz 15%[======> ] 6.69M 2.27MB/s
so, how should i do to make the progress bar display like above
environments
- node.js: v14.16.1
- wget: GNU Wget 1.21.2 built on darwin21.1.0
sorry
my english is poor. please excuse grammar or typing error. i'm trying my best to imporve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑你得到的效果是由于 GNU Wget 认为它的
stderr
被重定向到文件。我不知道如何使用 GNU Wget 获得所需的效果,但如果您只需要下载文件(并且不需要使用 GNU Wget 这样做),那么请考虑将https
与进度
。最后需要先安装,然后你就可以像这样使用它(改编自
progress
- 下载示例)代码下载 www.example.com 页面并保存作为
index.html
,请注意,与 wget 的 url 不同,您不在主机中包含协议(在本例中为https
)。443
是 https 的默认端口。/
是服务器上资源的路径(在本例中:主页)。Content-Length
标头保存有关文件总大小的信息。I suspect effect you did get is due to GNU Wget thinking its'
stderr
was redirected to file. I do not know how to get desired effect using GNU Wget, but if you need to just download file (and are not required to do so using GNU Wget) then consider combininghttps
withprogress
. Last need to be installed first, dothen you might use it like so (adapted from
progress
- download example)Code do download www.example.com page and save it as
index.html
, note that unlike url for wget you do NOT include protocol (in this casehttps
) in host.443
is default port for https./
is path to resource on server (in this case: main page).Content-Length
header hold information about total file size.