为什么 Blueimp 的 JQuery 文件上传添加所有 prev.即使在初始化时设置了选项 [replaceFileInput: false] 和 [maxNumberOfFiles: 1] 也可以选择文件? [英] Why Blueimp's JQuery File Upload add all prev. selected files even if options [replaceFileInput: false] and [maxNumberOfFiles: 1] are set on init?

查看:20
本文介绍了为什么 Blueimp 的 JQuery 文件上传添加所有 prev.即使在初始化时设置了选项 [replaceFileInput: false] 和 [maxNumberOfFiles: 1] 也可以选择文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有一个带有一些输入字段和一个文件输入字段的普通表单.我使用 Blueimp 的 Jquery File Upload 插件来上传文件.如果您选择一个文件,然后单击上传按钮,它似乎可以工作.但是如果您重新选择要上传的文件,它会保存所有选择的历史,并在上传后将所有 XHR 发送到服务器.

I have just normal form with some input fields and one file input field. I use the Blueimp's Jquery File Upload plugin to upload a file. It seems to work, if you select a file and after that click the upload button. But if you reselect files to upload, it saves all the prehistory of selections and after upload sends all the XHRs to the server.

我只想上传一个当前选择的文件,不是所有之前选择的文件(在文件打开对话框中).

I want to upload only one currently selected file, not all the previously selected files (in file open dialog).

这是我处理上传的 js 模块:

Here is my js module to handle the upload:

$(function () {
    $('#upload_form').fileupload({

        dataType: 'json',
        autoUpload: false,
        fileInput: '#filechose_button',
        replaceFileInput: false,
        maxNumberOfFiles: 1,
        multipart: true,

        add: function (e, data) {

            $('#upload_button').click(function () {

                $('#upload_button').attr('disabled', true);
                ...
                data.submit();
                ...
            });
        },

        done: function (e, data) {
          ... // successfully uploaded
        },

        progressall: function (e, data) {
          ... // update a progress bar
        }
    });
});

我在这里找到的解决方案(如何使用blueimp文件上传插件只上传一次文件?)似乎不是最好的方法(我认为它很脏),因为仅仅取消绑定点击事件仍然不能解决收集所有问题以前选择的文件(某种内存泄漏)

The solutions I found here (How to upload a file only once with blueimp file upload plugin?) seems to be not the best way (I see it as dirty), because just unbinding the click event still doesn't solve the problem of collecting all previously selected files (kind of memory leaks)

maxNumberOfFiles: 1 选项对我不起作用.

推荐答案

我遇到了同样的问题,我的解决方案是取消绑定按钮的点击事件,并在调用添加事件时将其绑定回来.试试这个.

I had the same problem, my solution was to unbind the click event of my button and bind it back whenever the add event is called. Try This.

...

add: function (e, data) {

        $('#upload_button').unbind('click');
        data.context = $('#upload_button').bind('click', function () {
                    ...
                    data.submit();
        }
}

这篇关于为什么 Blueimp 的 JQuery 文件上传添加所有 prev.即使在初始化时设置了选项 [replaceFileInput: false] 和 [maxNumberOfFiles: 1] 也可以选择文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆