AJAX - 执行的 shell 命令的进度条

发布于 2024-07-13 14:15:10 字数 450 浏览 5 评论 0 原文

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

下载是通过向 shell 输出百分比的脚本完成的。 我想使用 AJAX 将此信息传递回用户。 我怎样才能做到这一点?

感谢您的任何帮助和指导。

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

编辑 - 更多信息

这是一个 Linux Shell 命令 - Fedora Core 10。

目前,shell 输出如下所示:

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

百分比变化,我希望捕获它并在变化时将其发送回用户。

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

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.

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?

Thank you for any help and direction.

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.

EDIT - More Info

It is a Linux Shell command - Fedora Core 10.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情独悲 2024-07-20 14:15:10

您可以使用 popen 代替 exec。 这将为您提供一个与 fread 一起使用的句柄,以获取命令生成的输出。

您需要解析它对百分比指示器所做的更新。 获得该数据后,可以通过多种方式将其发送给客户端,例如使用“”风格的推送,或者通过 Ajax 请求轮询更新。

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.

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.

ぶ宁プ宁ぶ 2024-07-20 14:15:10

我没有尝试过,但我认为这种方法会起作用。

您需要三部分:

  1. 让 shell 脚本将其流输出到连接到端口的 netcat
  2. 让 php 脚本侦听来自所述端口的流以获取传入数据,更新 memcache 或某些数据库中的记录以及完成的百分比。
  3. 让您的 Web 脚本定期向服务器发出 ajax 调用,该服务器检查后端存储中的该值。

I haven't tried this, but I think this approach would work.

You need three pieces:

  1. Have shell script output its stream to netcat connected to a port
  2. Have a php script listening to stream coming from said port for incoming data, updating a record in memcache or some database w/ the percentage finished.
  3. Have your web script periodically make ajax calls, to the server which checks this value in your backend store.
意中人 2024-07-20 14:15:10

我正在研究类似的问题。 我必须解析视频转换 shell 脚本的输出。 我使用 popen 并解析返回的resource 的输出。 起初我使用了 fgets 但它无法将更新的值识别为新行。 因此,我创建了一个简单的函数,它需要一个可选的 $arg_delimiter ,以便您可以检查其他返回类型,例如 chr(13) 回车符。 示例代码进行了一些修改,因此未经测试,因为在我的例子中,这些函数是我的解析器对象上的方法。

function get_line ($arg_handle, $arg_delimiter = NULL)
{
  $delimiter = (NULL !== $arg_delimiter) ? $arg_delimiter : chr(10);
  $result    = array();

  while ( ! feof($arg_handle))
  {
    $currentCharacter = fgetc($arg_handle);

    if ($delimiter === $currentCharacter)
    {
      return implode('', $result);
    }

    $result[] = $currentCharacter;
  }

  return implode('', $result);
}

我只是像这样循环 popen() resource 的结果:

  $command = '/usr/bin/yourcommand';
  $handle  = popen($command . ' 2>&1', 'r');

  while ( ! feof($handle))
  {
    $line = get_line($handle, chr(13));

    preg_match($yourParserRegex, $line, $data);

    if (count($data) > 0)
    {
      printf("<script type='text/javascript'>\n  //<![CDATA[\n    window.alert('Result: %s');\n  // ]]>\n</script>"
            ,$data[1]
            );
      flush();
    }

  }

现在您需要做的就是找出彗星的内容。

I'm working on a similar problem. I have to parse the output of my video conversion shell script. I use popen and parse the output of the returned resource. At first I used fgets but that didn't recognize the updated values as new lines. So I created a simple function to that takes an optional $arg_delimiter so you can check for other return types like the chr(13) cariage return. The example code is a bit modified and therefor untested because in my case these functions were methods on my parser object.

function get_line ($arg_handle, $arg_delimiter = NULL)
{
  $delimiter = (NULL !== $arg_delimiter) ? $arg_delimiter : chr(10);
  $result    = array();

  while ( ! feof($arg_handle))
  {
    $currentCharacter = fgetc($arg_handle);

    if ($delimiter === $currentCharacter)
    {
      return implode('', $result);
    }

    $result[] = $currentCharacter;
  }

  return implode('', $result);
}

I simply loop over the results from the popen() resource like this:

  $command = '/usr/bin/yourcommand';
  $handle  = popen($command . ' 2>&1', 'r');

  while ( ! feof($handle))
  {
    $line = get_line($handle, chr(13));

    preg_match($yourParserRegex, $line, $data);

    if (count($data) > 0)
    {
      printf("<script type='text/javascript'>\n  //<![CDATA[\n    window.alert('Result: %s');\n  // ]]>\n</script>"
            ,$data[1]
            );
      flush();
    }

  }

Now all you need to do is figure out the comet stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文