3

情况:用户通过 AJAX 上传了许多照片,然后继续与网站交互,同时 PHP 脚本继续在后台运行并根据上传的照片生成各种缩略图。

站点配置:

  1. jQuery AJAX (v1.9.1)
  2. PHP 5.4.7,FastCGI 模式
  3. IIS 7.5,带有 gzip

我之前提到并尝试实施的帖子(但无济于事):

根据之前的帖子,我已经尝试了大量的脚本选项,但是似乎没有一个告诉 AJAX 脚本让用户继续,而 PHP 继续处理......

示例 PHP 代码:

<?php
   // Save images to db, etc

   // Now tell AJAX to let the user continue, before generating thumbnails
   if(ini_get('zlib.output_compression')) { 
       ini_set('zlib.output_compression', 'Off'); // turn IIS gzip for this file
   }
   ob_end_clean();
   header("Connection: close");
   header("Content-Encoding: none"); //ensures gzip is not sent through
   ob_start();
      echo '123'; // 3 digit number will be sent back to AJAX
   $size = ob_get_length(); // should mean Content-Length = 3
   header("Content-Length: $size");
   ob_end_flush(); 
   flush(); 
   ob_end_clean();

   // Generate thumbnails, etc
?>

示例 jQuery AJAX 代码:

$.ajax({
        type: 'POST',
        url: ajax_url,
        data: { foo: bar },
        beforeSend:function(){
            // 
        },
        success:function(data){
            alert(data); // Only seems to be firing once the thumbnails have been generated.
        }
    });

响应标头似乎还可以...

Firebug 检测到的 POST 响应标头

问题:如何让 AJAX 允许用户在从 PHP 脚本中间收到代码后继续,而 PHP 脚本继续生成缩略图?

4

1 回答 1

1

If you run request, it will always wait until PHP Script finish executing, or there will be a timeout. So you cannot stop AJAX in middle, and keep PHP running. If you want to upload files, and then create thumbnails, but have info that files are uploaded, do it in two steps:

  1. upload files with AJAX -> return success
  2. run another AJAX request on success to get uploaded images (or thumbs in fact).

Thanks to that, thumbs can be also rendered later, when they are first time requested (even without ajax). If you don't want requesting and waiting for thumbs, use cron job on server, which will create thumbs for awaiting images.

于 2013-03-11T11:24:00.877 回答