dongqu2863 2012-12-11 03:33
浏览 137
已采纳

中止上传请求

I'm having an issue where I am trying to support cancelling file uploads. I would like to know what the best practice for determine whether an upload is cancellable. So how can you determine when the file has completed uploading, versus the server generating/returning a response? I understand this is possible by tracking the file progress in HTML5, but since I have to support IE9, I am running out of ideas.

The end result is if you attempt to cancel a file upload that is nearing being completely upload, and issue the abort request, you end up aborting the response and the file is happily sitting on the server.

I am using jquery to submit the request, and am cancelling via the abort() method. I see in the browser console that the request was successfully aborted.

Am I missing something trivial?

  • 写回答

1条回答 默认 最新

  • drdyszuy488152 2013-05-06 20:15
    关注

    PHP now has the ability to track the upload progress. See "Session Upload Progress".

    Use this feature to write a short script: checkUpload.php, and use AJAX to return the status back to your IE9 page.

    <?php
    $_SESSION["upload_progress_123"] = array(
     "start_time" => 1234567890,   // The request time
     "content_length" => 57343257, // POST content length
     "bytes_processed" => 453489,  // Amount of bytes received and processed
     "done" => false,              // true when the POST handler has finished, successfully or not
     "files" => array(
      0 => array(
       "field_name" => "file1",       // Name of the <input/> field
       // The following 3 elements equals those in $_FILES
       "name" => "foo.avi",
       "tmp_name" => "/tmp/phpxxxxxx",
       "error" => 0,
       "done" => true,                // True when the POST handler has finished handling this file
       "start_time" => 1234567890,    // When this file has started to be processed
       "bytes_processed" => 57343250, // Number of bytes received and processed for this file
      ),
      // An other file, not finished uploading, in the same request
      1 => array(
       "field_name" => "file2",
       "name" => "bar.avi",
       "tmp_name" => NULL,
       "error" => 0,
       "done" => false,
       "start_time" => 1234567899,
       "bytes_processed" => 54554,
      ),
     )
    );
    

    Using PHP, it is also now possible to cancel the upload process. From that same manual page referenced above, comes the following text:

    It is also possible to cancel the currently in-progress file upload, by setting the $_SESSION[$key]["cancel_upload"] key to TRUE. When uploading multiple files in the same request, this will only cancel the currently in-progress file upload, and pending file uploads, but will not remove successfully completed uploads. When an upload is cancelled like this, the error key in $_FILES array will be set to UPLOAD_ERR_EXTENSION.

    The only thing not covered in the PHP documentation is how to get the total file size. There is a very good review of this process here: "PHP Master | Tracking Upload Progress"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了