1

首先,我想说我的英语不好,所以我想道歉。我的问题是我试图自定义 FineUploader FileTemplate 选项但没有成功。我不想使用fineUploaderBasic。我想要完全定制。首先,我试图在上传成功后隐藏文件名和大小并且我成功地做到了,但是当我尝试自定义删除按钮时,问题就开始了。上传后删除按钮出现但禁用,我无法单击它。下面是我的代码:

var restricteduploader = new qq.FineUploader({
                        element: $('#restricted-fine-uploader')[0],
                        text: {
                            uploadButton: '<div><i class="icon-upload icon-white"></i>Subir Imagen</div>',
                            deleteButton: '<input type="button" id="btnDelete" value="Eliminar imagen" />'
                        },

                        template:
                        '<div class="qq-uploader">' +
                            '<div class="qq-upload-drop-area"><span>{dragZoneText}</span></div>' +
                            '<div class="qq-upload-button">{uploadButtonText}</div>' +
                            '<span class="qq-drop-processing"><span>{dropProcessingText}</span><span class="qq-drop-processing-spinner"></span></span>' +
                            '<ul class="qq-upload-list"></ul>' +
                        '</div>',
                        fileTemplate:
                            '<li>' +
                                '<div class="qq-progress-bar"></div>' +
                                '<span class="qq-upload-spinner"></span>' +
                                '<span class="qq-upload-finished"></span>' +
                                '<span class="qq-edit-filename-icon"></span>' +
                                '<span class="hide-file"></span>' +
                                '<div>IMAGEN SUBIDA CON EXITO!!</div>' +
                                '<input class="qq-edit-filename" tabindex="0" type="text">' +
                                '<span class="hide-size"></span>' +
                                '<a class="qq-upload-cancel" href="#">{cancelButtonText}</a>' +
                                '<a class="qq-upload-retry" href="#">{retryButtonText}</a>' +
                                '<div class="qq-upload-delete">{deleteButtonText}</div>' +
                                '<span class="qq-upload-status-text">{statusText}</span>' +
                            '</li>',
                        classes: {
                            file: 'hide-file',
                            size: 'hide-size'
                        },
                        request: {
                            endpoint: '<%= Url.Action("UploadBatchDataFile", "Account") %>'
                        },
                        deleteFile: {
                            enabled: true,
                            endpoint: '<%= Url.Action("DeleteFile", "Account") %>',
                            method: 'POST'
                        },
                        multiple: false,
                        validation: {
                            allowedExtensions: ['jpeg', 'jpg', 'png'],
                            sizeLimit: 411062 // 50 kB = 50 * 1024 bytes
                        },
                        showMessage: function (message) {
                            $('#restricted-fine-uploader').append('<div class="alert-error">' + message + '</div>');
                        },
                        //listElement: document.getElementById('files'),
                        messages: { typeError : "{file} no es un tipo de imagen valido. Imagenes valida(s): {extensions}." },
                        callbacks: {
                            onSubmitDelete: function(event, id) {
                                var filename = $(this).fineUploader('getName', id);
                                $(this).fineUploader('setDeleteFileParams', {filename: filename}, id);
                            },
                            onComplete: function (id, filename, responseJSON) {
                                if (responseJSON.success) {
                                    $('div div.alert-error').remove();

                                    $('#imgUploaded').attr("src", "<%: Url.Content("~/Images/") %>" + responseJSON.filename);
                                    $('#hidImage').attr("value", "<%: Url.Content("~/Images/") %>" + responseJSON.filename);

                                }
                            }
                        }
                    });

对我来说,自定义 fileTemplate 非常困难,在此之前我试图使 FileTemplate 成为表格的一部分,在模板上更改它

'<ul class="qq-upload-list"></ul>' to '<table class="qq-upload-list"></table>'

这在文件模板上

'<li>' to '<tr><td>' and '</li>' to </td></tr>

但这又不起作用,在成功上传 FineUploader 后没有向我显示 FileTemplate。

4

1 回答 1

7

首先,在您的onComplete处理程序更改中,您有一个语法错误。改变

$('#imgUploaded').attr("src", "<%: Url.Content("~/Images/") %>" + responseJSON.filename);
$('#hidImage').attr("value", "<%: Url.Content("~/Images/") %>" + responseJSON.filename); 

$('#imgUploaded').attr("src", "<%: Url.Content('~/Images/') %>" + responseJSON.filename);
$('#hidImage').attr("value", "<%: Url.Content('~/Images/') %>" + responseJSON.filename);

其次,在您的text选项属性中,您提供的是 HTML,而不是您希望出现在按钮内的文本字符串。所以你应该改变,

text: {
    uploadButton: '<div><i class="icon-upload icon-white"></i>Subir Imagen</div>',
    deleteButton: '<input type="button" id="btnDelete" value="Eliminar imagen" />'
},

text: {
    uploadButton: 'Subir Imagen',
    deleteButton: 'Eliminar imagen'
},

如果您仍想添加上传图标(如我所见),或以任何其他方式自定义 HTML,请修改template选项属性

template:
    '<div class="qq-uploader">' +
        '<div class="qq-upload-drop-area"><span>{dragZoneText}</span></div>' +
        '<div class="qq-upload-button"><i class="icon-upload icon-white"></i>{uploadButtonText}</div>' +
        '<span class="qq-drop-processing"><span>{dropProcessingText}</span><span class="qq-drop-processing-spinner"></span></span>' +
        '<ul class="qq-upload-list"></ul>' +
        '</div>',

最后,您的 FineUploader 实例中似乎混合了 jQuery 和非 jQuery 代码。你知道FineUploader 有一个 jQuery 插件,对吧?它会让你的生活更轻松,它会让你的回调真正起作用,因为thisin的值

onSubmitDelete: function(event, id) {
    var filename = $(this).fineUploader('getName', id);
    $(this).fineUploader('setDeleteFileParams', {filename: filename}, id);
},

根本不会是 FineUploader 实例化的元素。如果您使用的是 jQuery 插件,那么它会起作用。

使用 jQuery 插件,您可以...

$("#restricted-fine-uploader").fineUploader({
    // .. define your options here, same as above ...
}).on('submitDelete', function (event, id) {
    var filename = $(this).fineUploader('getName', id);
    $(this).fineUploader('setDeleteFileParams', {filename: filename}, id);

}).on('complete', function (event, id, filename, responseJSON) {
    if (responseJSON.success) {
        $('div div.alert-error').remove();

        $('#imgUploaded').attr("src", "<%: Url.Content('~/Images/') %>" + responseJSON.filename);
        $('#hidImage').attr("value", "<%: Url.Content('~/Images/') %>" + responseJSON.filename);

    }
});

这是有关如何使用 jQuery 插件的文档的链接

更新

这里有更多代码准确显示如何 a) 正确使用 jQuery 插件和事件,以及 b) 如何使用input元素作为删除按钮。

注意:这需要 FineUploader >=3.7.1

JavaScript

$(function () {
    $("#restricted-fine-uploader").fineUploader({
        text: {
            uploadButton: "<i class='icon-upload icon-white'></i>Subir Imagen"
        },
        fileTemplate:
            '<li>' +
            '<div class="qq-progress-bar"></div>' +
            '<span class="qq-upload-spinner"></span>' +
            '<span class="qq-upload-finished"></span>' +
            '<span class="hide-file"></span>' +
            '<div>IMAGEN SUBIDA CON EXITO!!</div>' +
            '<span class="hide-size"></span>' +
            '<a class="qq-upload-cancel" href="#">{cancelButtonText}</a>' +
            '<a class="qq-upload-retry" href="#">{retryButtonText}</a>' +
            '<input class="qq-upload-delete" type="button" value="{deleteButtonText}" />' +
            '<span class="qq-upload-status-text">{statusText}</span>' +
            '</li>',

        classes: {
            file: 'hide-file',
            size: 'hide-size'
        },
        request: {
            endpoint: '<%= Url.Action("UploadBatchDataFile", "Account") %>'
        },
        deleteFile: {
            enabled: true,
            endpoint: '<%= Url.Action("DeleteFile", "Account") %>',
            method: 'POST'
        },
        multiple: false,
        validation: {
            allowedExtensions: ['jpeg', 'jpg', 'png'],
            sizeLimit: 411062 // 50 kB = 50 * 1024 bytes
        },
        showMessage: function (message) {
            $('#restricted-fine-uploader').append('<div class="alert-error">' + message + '</div>');
        },
        //listElement: document.getElementById('files'),
        messages: {
            typeError: "{file} no es un tipo de imagen valido. Imagenes valida(s): {extensions}."
        }
    }).on('submitDelete', function (event, id) {
        var filename = $(this).fineUploader('getName', id);
        $(this).fineUploader('setDeleteFileParams', {
            filename: filename
        }, id);
    }).on('complete', function (id, filename, responseJSON) {
        if (responseJSON.success) {
            $('div div.alert-error').remove();

            $('#imgUploaded').attr('src', '<%: Url.Content("~/Images/") %>' + responseJSON.filename);
            $('#hidImage').attr('value', '<%: Url.Content("~/Images/") %>' + responseJSON.filename);

        }
    });
});

HTML

<ul id="restricted-fine-uploader"></ul>
于 2013-07-30T14:43:36.730 回答