如何在下面取消此文件上传

时间:2012-04-13 22:34:41

标签: javascript jquery file-upload

我有一个包含文件输入,上传按钮和取消按钮的表单:

<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='stopImageUpload(this);' class='imageuploadform' >
<p class='imagef1_upload_form' align='center'><label>Image File: <input name='fileImage' type='file' class='fileImage' /></label>
<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>
</p>
<p class='imagef1_cancel' align='center'>
<label><input type='button' name='imageCancel' class='imageCancel' value='Cancel' /></label>
</p> 
<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe>
</form>

我已经设置了php来上传文件,所有这些和上传工作完美。由于下面的javascript函数,上传开始和停止但我的问题是如何编码以便当用户开始上传(startImageUpload()函数)时,如果用户希望在上传期间通过单击取消上传取消按钮,然后停止上传(我相信它需要导航到stopImageUpload()函数,但我不确定这是否正确)。

我已经设置了正确显示在屏幕上的所有内容,我只需要知道什么是正确的功能,以便能够成功取消上传以及显示消息'上传过程中出现错误'已经是在stopImageUpload()函数中。

以下是上传开始的功能:

function startImageUpload(imageuploadform){

  $(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
  $(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
  $(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
  sourceImageForm = imageuploadform;
      return true;
}

以下是上传完成的功能:

function stopImageUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
      }
      $(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
      $(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');

      return true;   
}

1 个答案:

答案 0 :(得分:3)

好的,所以你已经拥有了这个函数和click处理程序,那么是什么阻止你在事件处理程序中调用你的函数呢?

假设stopImageUpload函数在同一个文件中(或至少在该页面上加载):

$(".imagecancel").on("click", function(event) {
    console.log("clicked");
    event.preventDefault();
    stopImageUpload(); // Just call it?!
});

另外,请确保浏览器停止在stopImageUpload函数中发送当前请求(上传):

if(navigator.appName == "Microsoft Internet Explorer") {
    window.document.execCommand('Stop');
} else {
    window.stop();
}