简体   繁体   English

如何在jQuery中取消由ajaxSubmit()启动的文件上传?

[英]How do I cancel a file upload started by ajaxSubmit() in jQuery?

I have code similar to the following: 我有类似以下代码:

UploadWidget.prototype.setup_form_handling = function() {
    var _upload_widget = this;
    $('form#uploader')
    .unbind('trigger-submit-form')  // This might be our company's own method
    .bind('trigger-submit-form', function() {
        var $form = $(this);
        $form.ajaxSubmit({
            dataType: 'json',
            success: function(data, status, xhr, form) {
                // ...
            },
            error: function(xhr, status, errorThrown) {
                // ...
            }
        });
        return false;
    });
};

Is there a way to use, say, the reset button of the form to cancel the upload process? 有没有办法使用表格的重置按钮来取消上传过程? Or would I have to navigate to the current page (refresh) in order to stop everything? 或者我是否必须导航到当前页面(刷新)才能停止一切?

I tried making a variable stored by the UploadWidget object that stores the jqXHR value (and calling var _upload_widget.jqXHR = $form.ajaxSubmit({ ... }); ), but I don't think I'm doing it right. 我尝试创建一个由UploadWidget对象存储的变量,该变量存储jqXHR值(并调用var _upload_widget.jqXHR = $form.ajaxSubmit({ ... }); ),但我不认为我做得对。

Unlike jQuery.ajax() , ajaxForm() and ajaxSubmit() of the jQuery form plugin do not return the jqXHR object. jQuery.ajax()不同,jQuery表单插件的ajaxSubmit() ajaxForm()ajaxSubmit()不返回jqXHR对象。 There are other ways to get the jqXHR object, though: 但是还有其他方法可以获取jqXHR对象:

data() method on the form object (easiest way) 表单对象上的data()方法(最简单的方法)

ajaxSubmit() returns the form object. ajaxSubmit()返回表单对象。 You can call the data() method on this form object to get the XHR object: 您可以在此表单对象上调用data()方法以获取XHR对象:

var form = $('#myForm').ajaxSubmit({ ... });
var xhr = form.data('jqxhr');

Then call xhr.abort() when you want to abort the upload. 然后在想要中止上传时调用xhr.abort()

This functionality was added in December 2012; 此功能已于2012年12月添加; see issue 221 . 问题221 (shukshin.ivan's answer was the first to point this out.) (shukshin.ivan的回答是第一个指出这一点。)

beforeSend callback (for jQuery Form versions before December 2012) beforeSend回调(对于2012年12月之前的jQuery Form版本)

In addition to the options listed in the jQuery form plugin site , ajaxForm() and ajaxSubmit() will also take any of the jQuery.ajax() options . 除了jQuery表单插件站点中列出选项之外, ajaxForm()ajaxSubmit()还将采用任何jQuery.ajax()选项 One of the options is a beforeSend callback, and the callback signature is beforeSend(jqXHR, settings) . 其中一个选项是beforeSend回调,回调签名是beforeSend(jqXHR, settings)

So, specify a beforeSend callback function, and the jqXHR object will be passed in as the first parameter of that callback. 因此,指定一个beforeSend回调函数,而jqXHR对象将作为回调的第一个参数传递英寸 Save that jqXHR object, and then you can call abort() on that jqXHR object when you want to abort the upload. 保存该jqXHR对象,然后当您想要中止上传时,可以在该jqXHR对象上调用abort()

Since Dec 2012 it is possible to access xhr directly: 自2012年12月起,可以直接访问xhr:

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

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

You can use this way: 你可以这样使用:

var myForm  = null; 
myForm = $('#myForm').ajaxSubmit({
               url: ajaxUrl,
               beforeSubmit : function() {
                   if(myForm != null)
                      myForm.data('jqxhr').abort();
                   },
               ...
          });

This will stop your previous ajaxSubmit requests. 这将停止您之前的ajaxSubmit请求。

Try 尝试

window.stop();

which mimics the stop button being pressed. 模仿按下的停止按钮。 Not entirely sure that IE likes it though. 不完全确定IE喜欢它。 For IE, you could try: 对于IE,您可以尝试:

document.execCommand('Stop');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM