FineUploader:在模板标记中添加上传按钮

时间:2015-11-01 00:13:39

标签: jquery fine-uploader

使用fineUploader jQuery插件,我试图在我的fineUploader模板中添加几个上传按钮。

但似乎在将模板添加到DOM之前,extraButtons选项不会在模板内查找元素,只在DOM内部。

http://docs.fineuploader.com/features/extra-buttons.html

因此,如果extraButtons元素位于模板内,则fineUploader无法找到它们。

我不想在我的模板外添加我的按钮... fineUploader可以在模板中定位extraButtons吗?

我的fineUploader instanciation看起来像这样:

$("#fine-uploader").fineUploader({
        debug : true,
        extraButtons: [
            {element: $('#upload-pictures-btn1')},
            {element: $('#upload-pictures-btn2')},
            {element: $('#upload-pictures-btn3')}
        ],
        request: {
            endpoint: '/publish/upload'
        }
    });

我的fineUploader模板如下所示:

<script type="text/template" id="qq-template">
<div class="qq-uploader-selector qq-uploader">
    <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
        <span class="qq-upload-drop-area-text-selector"></span>
    </div>
    <div class="qq-upload-button-selector qq-upload-button">
        Add file
    </div>

    <div class="upload-pictures-btns">
        <div id="upload-pictures-btn1"></div>
        <div id="upload-pictures-btn2"></div>
        <div id="upload-pictures-btn3"></div>
    </div>

    <span class="qq-drop-processing-selector qq-drop-processing">
        <span>Uploading ...</span>
        <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
    </span>
    <ul class="qq-upload-list-selector qq-upload-list" aria-live="polite" aria-relevant="additions removals">
        <li>
            <div class="qq-progress-bar-container-selector">
                <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-progress-bar-selector qq-progress-bar"></div>
            </div>

            <span class="qq-upload-thumb">
                <span class="qq-upload-spinner-selector qq-upload-spinner"></span>
                <img class="qq-thumbnail-selector" qq-max-size="365" qq-server-scale>
            </span>

            <span class="qq-upload-info">
                <span class="qq-upload-file-selector qq-upload-file"></span>
                <span class="qq-edit-filename-icon-selector qq-edit-filename-icon" aria-label="Edit filename"></span>
                <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
                <span class="qq-upload-size-selector qq-upload-size"></span>
            </span>

            <span class="qq-btns-wrapper">
                <button type="button" class="btn qq-btn qq-upload-cancel-selector qq-upload-cancel">Annuler</button>
                <button type="button" class="btn qq-btn qq-upload-retry-selector qq-upload-retry">Re-essayer</button>
                <button type="button" class="btn qq-btn qq-upload-delete-selector qq-upload-delete">Supprimer</button>
            </span>

            <span role="status" class="qq-upload-status-text-selector qq-upload-status-text"></span>
        </li>
    </ul>

</div>

实例时的JS错误:Uncaught TypeError: Cannot read property 'style' of undefined

如果我将3个额外的按钮移出模板,并将它们直接放在我的HTML标记中,它就可以了。但我需要他们在模板中。

1 个答案:

答案 0 :(得分:3)

您的代码存在一个问题。在Fine Uploader甚至初始化之前,您正在从DOM中选择元素。当然,在创建Fine Uploader的实例之前,这些元素尚不存在,因为您已在模板中定义了它们。

目前,在设置Element选项时,您必须extraButton传递给Fine Uploader,但有an open proposal to make this a bit more flexible。与此同时,一种可能的解决方法是在用户选择一个或多个文件后创建自己的<input type="file">然后submitting it to Fine Uploader via the API。例如:

<input type="file" name="extraButton">
<script>
document.querySelector('input[name="extraButton"]').addEventListener('change', function(event) {
   uploader.addFiles(event.target);
});
</script>