CakePHP、AJAX - 显示在后台运行的方法的进度

发布于 2024-11-09 02:47:09 字数 827 浏览 0 评论 0原文

我不知道该怎么做。

我有一个小型画廊脚本的管理面板。您创建图库,然后通过 FTP 上传图像(到指定文件夹,每个图库 100 张图像,使用 FTP 更容易)。

在画廊列表中,每个画廊旁边都有一个“创建拇指”链接,可转到 /admin/images/thumbnails/gallery_id。一切正常,正在创建缩略图,该方法重定向到带有 setFlash 信息的列表页面,表明它们已创建。

但问题是,创建这些小图像需要时间,如果超过十张图片 - 等待浏览器重新加载可能会很烦人。

我想添加某种进度条。或者可能只是已处理的图像的计数。

该方法基本上如下所示(其中还有更多内容,但不相关):

function admin_thumbnails($gallery_id)
  {
  $files = $this->File->listDir('/example/www/directory');

  foreach ($files AS $file)
    {
    $this->Attachment->thumbnail('/example/www/directory/'.$file, '/thumbs/', '150', '100', 'resizeCropTop');
    }

  $this->redirect(array('controller' => 'galleries', 'action' => 'index'));
  }

我可以在 foreach 循环中运行某些内容,但是什么呢?以及如何向用户展示?

任何指点都会很棒。

问候, 保罗

I'm not sure how to go about this.

I have an administration panel for a small gallery script. You create galleries, and then upload images through FTP (to a specified folder, with 100 images for every gallery, its just easier with FTP).

On the list of galleries, next to each one, there is a "create thumbs" link, which goes to /admin/images/thumbnails/gallery_id. Everything is working well, the thumbnails are being created, the method redirects to the list page with setFlash info, that they were created.

But the thing is, creating those small images takes time, and if it's more than ten pictures - it can be annoying to just wait for the browser to reload.

I would like to add a progress bar of some sort. Or maybe just a count of the images that were already processed.

The method basically looks like this (there more in it, but not relevant):

function admin_thumbnails($gallery_id)
  {
  $files = $this->File->listDir('/example/www/directory');

  foreach ($files AS $file)
    {
    $this->Attachment->thumbnail('/example/www/directory/'.$file, '/thumbs/', '150', '100', 'resizeCropTop');
    }

  $this->redirect(array('controller' => 'galleries', 'action' => 'index'));
  }

I could run something in the foreach loop, but what? And how to show it to the user?

Any pointers would be fantastic.

Regards,
Paul

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

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

发布评论

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

评论(2

帅气尐潴 2024-11-16 02:47:09

这不是一件简单的事情。 MVC 框架需要在发送 HTTP 响应之前渲染整个视图。你有两个选择。

首先,您可以使用 javascript 通过 ajax 调用此函数并显示进度指示器(动画 gif),让用户知道正在发生的事情。它不会告诉用户服务器在进程中的位置,它只会让他们知道正在完成工作。这既便宜又简单,有大量的例子。

第二个选项是开始缩略图创建,然后通过 ajax“轮询”服务器以了解其进度。您必须考虑服务器在创建缩略图时如何报告其进度。您还应该能够通过谷歌搜索找到相关示例。这是 另一个类似的问题

This isn't a straight forward thing to do. MVC frameworks need to render the entire view before they send the HTTP response. You have two options.

First, you can just use javascript to call this function via ajax and display a progress indicator (animated gif) to let the user know something is happening. It won't tell the user where in the process the server is, it will just let them know there is work being done. That is cheap and easy, tons of examples out there for that.

The second option is to kick off the thumbnail creation and then "poll" your server, via ajax, for it's progress. You would have to think about how your server could report back how it's progressing as it's creating the thumbnails. You should also be able to find examples of that by googling. Here is another SO question that is similar.

凶凌 2024-11-16 02:47:09

一个好的选择是在 foreach 之前创建一个临时文件,然后在每次(成功?)迭代中追加一行。在 js 中,使用简单的 poll 函数计算行数/标记数/其他内容的数量并绘制进度条。该过程完成后,删除临时文件。

我会这样解决。

A good option could be creating a temporary file before the foreach, then in every (succesful?) iteration apending a line. In js with a simple poll function count the number of lines/tokens/whatever and draw the progress bar. When the process is done, delete the temp file.

I would solve it that way.

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