2012-12-30 107 views
2

當文件成功上傳時,顯示正確的消息時出現問題。成功上傳成功消息後取消消息

會發生什麼事是,當一個文件被成功上傳,不到一秒鐘,它會顯示成功的消息,但隨後突然它改變取消的消息。我的問題是,如果文件成功上傳,我希望顯示成功消息,顯然我不希望它更改爲取消消息。

下面是一個包含文件輸入以及上傳和取消按鈕的表單;

<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

代碼調用:

stopImageUpload(2); 

Ajax調用完成後。通過2會導致您的代碼說上傳被取消。所以,現在代碼完全按照您要求的方式執行(即始終說上傳已取消)。

您需要區分ajax函數可能完成的成功和其他方式,以便在完成時顯示正確的狀態。