开发者

How can I cancel a file upload using the jQuery forms plugin

开发者 https://www.devze.com 2023-04-13 00:31 出处:网络
I\'m using the jQuery Forms plugin to do an asynchronous file upload. Currently, the only way for the user to abort that upload is to press the \"Stop\" button on their browser, which may cause other

I'm using the jQuery Forms plugin to do an asynchronous file upload. Currently, the only way for the user to abort that upload is to press the "Stop" button on their browser, which may cause other javascript and ajax requests to stop as well. I'd like to provide a ca开发者_开发技巧ncel button, but I haven't found any way to cancel an upload as it is happening using the API.

Is there a built-in way, or at least a robust hack I can use, to cancel the upload as it occurs?


The ajaxForm method takes all the same options that jQuery.ajax takes, so it is possible to capture the dummy xhr that it produces in a beforeSend handler. The xhr has an abort method that aborts the file upload:

f.ajaxForm({
    beforeSend: function(xhr) {
        cancelBtn.click(xhr.abort);
    }});


Since Dec 2012 it is possible to access xhr directly:

var form = $('#myForm').ajaxSubmit({ ... });
var xhr = form.data('jqxhr');
....
$('#cancel').on('click', function(){
    xhr.abort();
});

I use it to cancel ajax file upload.

link - https://github.com/malsup/form/issues/221

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号