0

Internet Explorer 在取消文件上传时给我带来了问题,但在所有其他浏览器中,在取消文件时它不会引起任何问题。

现在在其他浏览器中,如果我上传文件,然后在上传过程中单击“取消”按钮,它将停止文件上传到服务器,显示取消消息,就是这样,没有其他任何事情发生,这很好。

但是在interent explorer中,如果我上传一个文件,然后在上传过程中点击“取消”按钮,它会显示取消消息,但它仍在后台上传文件,这意味着文件将上传到服务器和然后它将显示文件加载成功消息,替换取消消息。

我的问题是,我的代码中的什么导致 Internet Explorer 出现问题,即使我已经取消了上传,它也会继续上传文件。有谁知道如何解决这个问题?

下面是一个包含文件输入以及上传和取消按钮的表单;

<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target_image' onsubmit='return imageClickHandler(this);' class='imageuploadform' > 
 Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>
 <input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>
 <input type='reset' name='imageCancel' class='imageCancel' value='Cancel' /></label>
 <iframe class='upload_target_image' name='upload_target_image' src='#' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>    

下面是开始上传文件的函数,其中包含取消函数:

 function startImageUpload(imageuploadform){

  $(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
  sourceImageForm = imageuploadform;

        $(imageuploadform).find(".imageCancel").on("click", function(event) {
                  $('.upload_target_image').get(0).contentwindow
              $("iframe[name='upload_target_image']").attr("src", "javascript:'<html></html>'");
               jQuery.ajax("cancelimage.php").done(function(data) {

        return stopImageUpload(2);
    });  

}); 

      return true;
}

以下是上传完成后显示相关消息的代码:

      var imagecounter = 0;

function stopImageUpload(success, imagefilename){

      var result = '';
      imagecounter++;

      if (success == 1){
         result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span>';      
      }
      else if (success == 2){
          result = '<span class="imagemsg'+imagecounter+'"> The file upload was cancelled</span>';
      }
      else {
         result = '<span class="imagemsg'+imagecounter+'">There was an error during file upload</span>';
      }


      $(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
      $(sourceImageForm).find('.imagemsg').html(result);
      $(sourceImageForm).find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");


      return true;   
}

下面是上传文件的 imageupload.php 页面:

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);


session_start();


if ($_FILES['fileImage']['error'] === UPLOAD_ERR_OK) {
    $result = 0;


    if (getimagesize($_FILES['fileImage']['tmp_name'])) {
        if ((($_FILES["fileImage"]["type"] == "image/gif") || ($_FILES["fileImage"]["type"] == "image/jpeg") || ($_FILES["fileImage"]["type"] == "image/pjpeg") || ($_FILES["fileImage"]["type"] == "image/jpg")) && ($_FILES['fileImage']['size'] > 0)) {
            if (is_file("ImageFiles/" . $_FILES['fileImage']['name'])) {
                $parts = explode(".", $_FILES['fileImage']['name']);
                $ext   = array_pop($parts);
                $base  = implode(".", $parts);
                $n     = 2;

                while (is_file("ImageFiles/" . $base . "_" . $n . "." . $ext))
                    $n++;
                $_FILES['fileImage']['name'] = $base . "_" . $n . "." . $ext;

                move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
                $result = 1;


            }

            else {
                move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
                $result = 1;
            }

        }

    }
} else {
    echo "Upload was not successful";
}

?> 

  <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php
echo $result;
?>, '<?php
echo $_FILES['fileImage']['name'];
?>');</script> 



</body> 
</html>

更新:

以下是您所说的正确:

//startImageUpload

    function startImageUpload(imageuploadform){

      $(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
      sourceImageForm = imageuploadform;

            $(imageuploadform).find(".imageCancel").on("click", function(event) {
        $('.upload_target_image').get(0).contentwindow
        $("iframe[name='upload_target_image']").attr("src", "javascript:'<html></html>'");
         $request = $.ajax("cancelimage.php").done(function(data) {

            return stopImageUpload(2);

        });  

    }); 

          return true;
    }

//imageClickHandler

 function imageClickHandler(imageuploadform){ 
      if(imageValidation(imageuploadform)){ 
          window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform); 
          return startImageUpload(imageuploadform); 
          $request.abort()
      } 
      return false;
  }
4

1 回答 1

1

看来我错误地认为您使用 ajax 上传图像。现在我看到了完整的代码,我意识到你只是允许表单提交,而 ajax 只是与接收取消的 php 进行通信。这意味着您不能使用 ajax abort 来取消上传。您可以做的是将 iframe 内容替换为 cancelimage.php。我没有测试过这个

//startImageUpload

function startImageUpload(imageuploadform){

  $(imageuploadform).find('.imagef1_cancel').css('visibility','visible');

  sourceImageForm = imageuploadform; // not sure what this does

  $(imageuploadform).find(".imageCancel").on("click", function(event) {

    $('.upload_target_image').get(0).contentwindow; // not sure what this is doing....

    $("iframe[name='upload_target_image']").on("load",function() {
      stopImageUpload(2);
    }).attr("src", "cancelimage.php")

  }); 

  return true;
}

//imageClickHandler

function imageClickHandler(imageuploadform){ 
  if(imageValidation(imageuploadform)){ 
      window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform); 
      return startImageUpload(imageuploadform); 
  } 
  return false;
}
于 2012-12-29T07:12:40.967 回答