成功上传后,成功消息将更改为取消消息

时间:2012-12-30 00:45:57

标签: php jquery html

在文件成功上传时,我无法显示正确的消息。

当文件成功上传时,会在一瞬间显示成功消息但突然变为取消消息。我的问题是,如果文件成功上传,我希望显示成功消息,显然我不希望它更改为取消消息。

下面是一个表格,其中包含文件输入以及上传和取消按钮;

<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>'");
     $request = $.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;   
}

以下是clickHandler函数:

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

下面是上传文件的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";
}

?> 

1 个答案:

答案 0 :(得分:1)

您的代码致电:

stopImageUpload(2);

完成ajax调用后。传递2会导致您的代码说上传已取消。所以,现在代码完全按照你的要求去做(总是说上传被取消了)。

您需要区分成功和ajax功能可能完成的其他方式,以便在完成时显示正确的状态。