backbone.js 在为现有文件发送Ping服务器之前上载JQuery文件

oiopk7p5  于 2022-11-24  发布在  jQuery
关注(0)|答案(1)|浏览(104)

我是Jquery文件上传的新手,它工作得很好。但是我想知道是否有人知道一个好方法来阻止发送上传,直到可以发送GET请求来 * 验证文件是否已经存在 *?
我正在使用backbone.js,并且已经尝试将拦截附加到提交表单事件和文件输入更改事件。两者都被绕过。
目前的外观如下所示:

events: {
        'change form.upload-file input': 'checkExist'
    },

    checkExist: function (e) {
        return false;
    },

    initialize: function () {
        _.bindAll(this);

        this.$('.upload-file').fileupload({
            dataType: 'json',
            method: 'PUT',
            add: this.add,
            progress: this.progress,
            done: this.done,
            fail: this.fail,
            always: this.always
        });

        return __super__.initialize.apply(this, arguments);
    },
bxjv4tth

bxjv4tth1#

我只是通过手头有一个文件列表并停止xhr(如果包括的话)来解决这个问题

beforeSend : function(xhr, opts){
                var q = 'Do you want to replace ' + opts.files[0].name + '?';
                if( that.checkExist(xhr, opts) && !window.confirm(q))  {
                    xhr.abort();
                }
            },

如果能知道一种异步的方法,那就太好了。

相关问题