AJAX-执行的Shell命令的进度条 [英] AJAX - Progress bar for a shell command that is executed

查看:68
本文介绍了AJAX-执行的Shell命令的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网站上使用AJAX,我想向用户显示服务器正在下载的文件的进度.

I am making use of AJAX on my site and I would like to show users progress of a file that is being downloaded by my server.

下载是通过脚本完成的,该脚本将百分比输出到外壳.我想使用AJAX将此信息传递回用户.我该怎么办?

The download is done by script that outputs a percentage to the shell. I would like to pass this info back to the user using AJAX. How can I do this?

感谢您的帮助和指导.

我希望您的解决方案不涉及写入文本文件并从文本文件中检索该百分比!!我想太多了.

I hope your solutions do not involve writing to a text file and retrieving that percentage from the text file!! Too much over head I think.

这是Linux Shell命令-Fedora Core 10.

It is a Linux Shell command - Fedora Core 10.

当前,这是shell输出的样子:

Currently this is how the shell output looks like:

[download]   9.9% of 10.09M at 10.62M/s ETA 00:00

百分比发生变化,我希望捕获该百分比并将其发送给用户.

The percentage changes and I wish to capture that and send it back to the user as it changes.

要执行此操作,我使用了PHP的exec()函数.

To execute this, I make use of PHPs exec() function.

推荐答案

您可以使用 popen .这将为您提供一个与fread一起使用的句柄,以在命令发生时获取命令生成的输出.

Instead of exec, you could use popen. This will give you a handle you use with fread to grab the output your command generates as it happens.

您需要解析其对百分比指标的更新.有了这些数据后,您可以通过多种方式将其发送给客户端,例如使用" comet "样式推送,或者使用Ajax请求轮询进行更新.

You'll need to parse out the updates it makes to the percentage indicator. Once you have that data, there are a few ways you could get it to a client, e.g. with a "comet" style push, or have an Ajax request poll for updates.

这篇关于AJAX-执行的Shell命令的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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