如何通过单击“取消”按钮取消文件上载

时间:2012-04-14 13:13:49

标签: javascript jquery button file-upload

我有一个表单,其中包含表单中的文件输入,更重要的是取消按钮,该按钮应取消文件上传:

var $fileImage = $("<form action='imageupload.php' method='post' class='imageuploadform' ...><label>" + 

"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label>" +

"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 

"</p><p class='imagef1_cancel' align='center'><label>" +

"<input type='button' name='cancelImageBtn' class='cancelimage' value='Cancel' /></label>" + 
"</p></form>");

现在,我已经完成了将文件上传到服务器的所有内容。但我有一个问题。

我遇到的问题是我不知道如何取消和上传。目前,如果用户点击“取消”按钮,则没有任何反应。我想要发生的是,如果用户点击“取消”按钮,它将导航到“stopImageUpload()”函数,并停止文件上传。但是我怎么能这样做呢?

下面是我尝试创建取消按钮功能,但是当点击“取消”按钮时,没有任何反应。

   $(".cancelimage").on("click", function(event) {
    console.log("clicked");
    event.preventDefault();
    stopImageUpload(success);
     });

下面是完整的代码,其中显示了取消按钮功能的放置位置以及开始上传和停止上传文件的功能:

          function startImageUpload(imageuploadform){

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

                $(".cancelimage").on("click", function(event) {
                console.log("clicked");
                event.preventDefault();
                stopImageUpload(success);
             });

                  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_upload_form').css('visibility','visible');


                  return true;   
            }

4 个答案:

答案 0 :(得分:3)

有一种可能的解决方案。你可以这样做:

$("#cancel").click(function(){
  $("#inputfile").remove();
  $("#containerof_inputfile").append("<input type=\"file\" id=\"inputfile\"/>");
});

答案 1 :(得分:1)

表单一旦开始提交,我不相信你可以在不导航到新页面的情况下停止它。

如果您将取消按钮重定向到同一页面,但在查询字符串中有一个标记,您可以使用该标记显示可能有效的取消通知。

答案 2 :(得分:1)

jQuery使这种事情变得更容易。您可以在返回的jQuery对象上调用abort。我想你可以查看jQuery代码,看看它是如何做到的,如果你真的想要自己动手。

编辑:我很好奇,looked it up。 Abort是Javascript中XMLHttpRequest函数的一种方法。

答案 3 :(得分:1)

这是可能会借给这里的人的答案。 (因为问题已经太老,所以无法完全回答问题)

我将回答如何实现取消上传或更多取消ajax请求。在许多应用程序中可能需要。

使用axios

您可以使用取消令牌取消请求。

  

axios取消令牌API基于撤回的可取消承诺提案。

您可以使用CancelToken.source工厂创建一个取消令牌,如下所示:

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get('/user/12345', {
  cancelToken: source.token
}).catch(function (thrown) {
  if (axios.isCancel(thrown)) {
    console.log('Request canceled', thrown.message);
  } else {
    // handle error
  }
});

axios.post('/user/12345', {
  name: 'new name'
}, {
  cancelToken: source.token
})

并触发取消

// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');

您还可以通过将执行程序函数传递给CancelToken构造函数来创建取消令牌:

const CancelToken = axios.CancelToken;
let cancel;

axios.get('/user/12345', {
  cancelToken: new CancelToken(function executor(c) {
    // An executor function receives a cancel function as a parameter
    cancel = c;
  })
});

// cancel the request
cancel();

Note: you can cancel several requests with the same cancel token.

doc https://github.com/axios/axios#cancellation

使用XMLHttpRequest:

我们使用xhr对象的 abort()方法。

var xhr = new XMLHttpRequest(),
    method = "GET",
    url = "https://developer.mozilla.org/";
xhr.open(method, url, true);

xhr.send();

if (OH_NOES_WE_NEED_TO_CANCEL_RIGHT_NOW_OR_ELSE) {
  xhr.abort();
}

doc https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort