1

我正在使用Blueimp jQuery File Upload,它工作正常,但我在取消其中一个文件的上传时遇到了问题。

当我添加一个文件时,我将一个带有一些额外输入和一个按钮的字段集附加到 html 中 - “取消”。我需要的是这样的:

$(".cancelUpload").on("click", function(){
      //remove this file from the queue, The data and the file will not be uploaded
})

这是我的代码的一部分

$('#MappeFile').fileupload({
        dataType : 'json',
        autoUpload : false,
        maxNumberOfFiles : undefined,
        maxFileSize : 2000,
        minFileSize : undefined,
        acceptFileTypes : /.+$/i,
        url : "/ajax/UploadFile.php",
        add : function(e, data) {
            $("#fileUploadButton").removeClass("toHide").on("click", function() {
                $('#progress .bar').show();
                if ($.browser.msie && parseInt($.browser.version, 10) < 10) {
                    $('#progress .bar').css({
                        "background" : "url(images/progressbar.gif) no-repeat",
                        "width" : "100%"
                    })
                } else {
                    $('#progress .bar').css({
                        'background-color' : "#2694E8",
                        'width' : '0%'
                    });
                }
                data.formData=$("form").serializeArray();
                data.submit();
                $("#fileUploadButton").off("click").addClass("toHide")

            })
        },
        change : function(e, data) {
            $.each(data.files, function(index, file) {

                filesCount++;
                vOutput = "<fieldset class='FileUploadFieldset'>"
                vOutput += "<legend>" + file.name + "</legend>"
                vOutput += "<label>Dateiname:</label><input type='text' value='"+file.name+"' name='Dateiname"+filesCount+"' size='50' class='UploadDateiName inputMaske'/><input type='text' class='toHide originName' name='originName"+filesCount+"' value='"+file.name+"'/>"
                vOutput += "<label>Dokumentdatum</label><input type='text' name='Datum"+filesCount+"' class='UploadDatum inputMaske'>"
                vOutput += "<label>Berater:</label><select name='Berater"+filesCount+"' class='UploadBeraterSelect inputMaske'></select>"
                vOutput += "<fieldset><legend>Kategorie:</legend><div class='UploadKategorie'></div></fieldset>"
                vOutput += "<fieldset><legend>Bemerkung:</legend><textarea name='Bemerkung"+filesCount+"' class='UploadBemerkung inputMaske'></textarea></fieldset>"
                vOutput += "<input type='button' class='cancelUpload neuButton' value='cancel'/>"
                vOutput += "</fieldset>"
                $("#MappeFilesToUpload").append(vOutput);   
            });
        }
    });
4

1 回答 1

1

如果您使用的是fileupload-ui.js,您只需要添加一个类“cancel”,它将自动绑定到 cancelHandler。

尝试更新这一行:

 vOutput += "<input type='button' class='cancelUpload neuButton' value='cancel'/>"

至:

    vOutput += '<div class="cancel"> <button> cancel </button> </div>'

如果您只使用核心插件,请查看此行以了解您需要做什么。

fileupload-ui.js

   _initEventHandlers: function () {
        this._super();
        this._on(this.options.filesContainer, {
            'click .start button': this._startHandler,
            'click .cancel button': this._cancelHandler,
            'click .delete button': this._deleteHandler
        });
        this._initButtonBarEventHandlers();
    },

    .
    .
    .

    _cancelHandler: function (e) {
        e.preventDefault();
        var template = $(e.currentTarget).closest('.template-upload'),
            data = template.data('data') || {};
        if (!data.jqXHR) {
            data.errorThrown = 'abort';
            this._trigger('fail', e, data);
        } else {
            data.jqXHR.abort();
        }
    },
于 2012-12-05T14:35:35.730 回答