【发布时间】:2014-05-12 03:35:21
【问题描述】:
关于如何在我的团队项目中上传流程的简要说明。
在客户端,我们通过 AJAX 调用上传服务到应用服务器。应用服务器然后通过Curl将该文件转发到文件服务器(文件服务器是私有的,只能由应用服务器访问)
情况是这样的。
文件上传时,已经通过应用服务器,到达Fileserver。但在数据传回客户端之前,用户单击客户端的取消按钮。
如何从应用服务器检查,如果用户中止请求,如果已经上传,则调用删除到文件服务器?
我的解决方案
-
如果php设置
ignore_user_abort=false,我无法检查上传是否取消。所以我在 curl 之前将其设置为true。ini_set('ignore_user_abort', TRUE);** 顺便说一句,
ignore_user_abort=false即使在脚本调用中止后也不会立即终止 curl 执行。 -
设置
CURLOPT_NOPROGRESS以跟踪呼叫是否中止的进度。curl_setopt($ch, CURLOPT_NOPROGRESS, false); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback_progress'); -
处理进度
$cancelled = false; //cancel flag function callback_progress($download_size, $downloaded, $upload_size, $uploaded){ //Really need this, if not connection_aborted() never be true and curl still running even if script aborted print " "; ob_flush (); flush (); if(connection_aborted()!= 0){ if(!$cancelled) $cancelled = true; return 0; //to continue script, and handle later } } -
继续
curl_close($ch); //close curl ini_set('ignore_user_abort', FALSE); //set back to false //If $cancelled if true, make delete call to file server using the file id if($cancelled && isset($response['id'])) return $this->removeFile($response['id']);
但是,这行不通。 $cancelled 仍然是 false,尽管在 callback_progress 函数中它已经是 true。
有没有更好的方法呢?对于这种情况,我在网络上的任何地方都找不到合适的解决方案。
【问题讨论】: