如何取消文件上传通过ajaxSubmit会()jQuery中开始了吗?文件上传、开始了、ajaxSubmit、jQuery

2023-09-11 00:54:55 作者:浅浅相思泪

我有code类似以下内容:

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?

我试图使由存储jqXHR值UploadWidget对象存储的一个变量(并调用 VAR _upload_widget.jqXHR = $ form.ajaxSubmit({...}); ),但我不认为我这样做是正确的。

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.

推荐答案

jQuery.ajax()当作ajaxForm() ajaxSubmit会()不返回jqXHR对象。还有另一种方式来获得jqXHR对象,但:

Unlike jQuery.ajax(), ajaxForm() and ajaxSubmit() of the jQuery form plugin do not return the jqXHR object. There is another way to get the jqXHR object, though:

在除了在jQuery的形式插件网站列出的选项,当作ajaxForm () ajaxSubmit会()也将采取任何的 jQuery.ajax()选项。其中一个选项是 beforeSend 回调,并且回调签名 beforeSend(jqXHR,设置)

In addition to the options listed in the jQuery form plugin site, ajaxForm() and ajaxSubmit() will also take any of the jQuery.ajax() options. One of the options is a beforeSend callback, and the callback signature is beforeSend(jqXHR, settings).

所以,指定 beforeSend 的回调函数,以及jqXHR对象将被传递作为回调的第一个参数。当你想放弃上传保存jqXHR对象,然后你可以调用中止()的jqXHR对象。

So, specify a beforeSend callback function, and the jqXHR object will be passed in as the first parameter of that callback. Save that jqXHR object, and then you can call abort() on that jqXHR object when you want to abort the upload.

 
精彩推荐
图片推荐