【问题标题】:FineUploader multiple instances and dynamic namingFineUploader 多实例和动态命名
【发布时间】:2014-06-03 21:28:24
【问题描述】:

我在使用 jquery 包装器的 MVC4 项目中使用 FineUploader 4.0.8。 Here 是我的 js 代码示例,它创建了一个fineUploader 的单个实例并且运行良好。此时,我需要多个fineUploader 实例,但每个单独的控件都不知道另一个控件,它们会根据需要在页面上呈现(我已经看到使用 jQuery .each 的先前问题,这在这里不起作用)。目前,我似乎无法正确呈现任何上传按钮,可能是由于 ID 重复或其他原因。请参阅下文,了解我如何使用 MVC 的 Razor 为该单个实例创建唯一变量和 html ID。

这是我当前的实现,我在其中添加了动态值(您看到 _@Model.{PropertyName} 的地方):

// Uploader control setup
    var fineuploader_@Model.SurveyItemId = $('#files-upload_@Model.SurveyItemId').fineUploader({
        debug: true,
        template: 'qq-template_@Model.SurveyItemId',
        button: $("#button_@Model.SurveyItemId"),
        request:
        {
            endpoint: '@Url.Action("UploadFile", "Survey")',
            customHeaders: { Accept: 'application/json' },
            params: {
                surveyInstanceId_@Model.SurveyItemId: (function () { return instance; }),
                surveyItemResultId_@Model.SurveyItemId: (function () { return surveyItemResultId; }),
                itemId_@Model.SurveyItemId: (function () { return itemId; }),
                loopingIndex_@Model.SurveyItemId: (function () { return loopingCounter++; })
            }
        },
        validation: {
            acceptFiles: ['image/*', 'application/xls', 'application/pdf', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'],
            allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'bmp', 'csv', 'xls', 'xlsx', 'pdf', 'xlt', 'xltx', 'txt'],
            sizeLimit: 1024 * 1024 * 2.5, // 2.5MB
            stopOnFirstInvalidFile: false
        },
        failedUploadTextDisplay: {
            mode: 'custom'
        },
        multiple: true,
        text: {
            uploadButton: 'Select your upload file(s)'
        }
    }).on('submitted', function (event, id, filename) {
        $("#modal-overlay").fadeIn();
        $("#modal-box").fadeIn();
        filesToUpload_@Model.SurveyItemId++;
        $(':input[type=button], :input[type=submit], :input[type=reset]').attr('disabled', 'disabled');
    }).on('complete', function (event, id, filename, responseJSON) {
        uploadedFileCounter_@Model.SurveyItemId++;
        if (filesToUpload_@Model.SurveyItemId == uploadedFileCounter_@Model.SurveyItemId) {
            $(':input[type=button], :input[type=submit], :input[type=reset]').removeAttr('disabled');
            //$("#overlay").fadeOut();
            $("#modal-box").fadeOut();
            $("#modal-overlay").fadeOut();
        }
    }).on('error', function (event, id, name, errorReason, xhr) {
        //$("#overlay").fadeOut();
        alert('error: ' + errorReason);
        $("#modal-box").fadeOut();
        $("#modal-overlay").fadeOut();
    });
});

使用与上面相同的原理,我也将这个逻辑添加到模板中。

编辑 - 在下面添加了整个模板:

 <script type="text/template" id="qq-template_@Model.SurveyItemId">
    <div class="qq-uploader-selector qq-uploader">
        <div class="qq-upload-drop-area-selector qq-upload-drop-area qq-hide-dropzone">
            <span>Drop files here to upload</span>
        </div>
        <div class="qq-upload-button-selector qq-upload-button">
            <div>Click here to select your upload file(s)</div>
        </div>
        <span class="qq-drop-processing-selector qq-drop-processing">
            <span>Processing dropped files...</span>
            <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
        </span>
        <ul class="qq-upload-list-selector qq-upload-list">
            <li>
                <div class="qq-progress-bar-container-selector">
                    <div class="qq-progress-bar-selector qq-progress-bar"></div>
                </div>                   
                <span class="qq-upload-file-selector qq-upload-file"></span>
                <span class="qq-upload-status-text-selector qq-upload-status-text"></span>
            </li>
        </ul>
    </div></script>

当我使用上面的代码时,我看到的只是拖放部分可见,没有按钮。也没有js错误。

我确实有一个示例,它上面只有一个此控件的实例,结果是相同的可见拖放部分,没有按钮)。有什么想法吗?如果您发现我遗漏了一些有用的东西,我会很乐意更新问题。如果这是我可以轻松改进或修复的问题,请不要只 -1 我。

【问题讨论】:

  • 您的模板包含无效的 html(缺少几个结束标签)。此外,您的模板中未引用该按钮。
  • 感谢您的浏览,但我确实说过这只是模板的一部分。明天我会将整个内容包含在编辑中。谢谢。
  • 当您要求解释奇怪的行为时,请不要在您的问题中包含无效的 HTML 或任何无效的代码。
  • 更新了模板,现在显示了整个内容。谢谢!

标签: jquery upload multiple-instances fine-uploader


【解决方案1】:

如果您没有看到上传按钮,则说明您的模板中没有正确标记的按钮。您提供的模板中似乎就是这种情况。如果您的实际模板确实包含正确标记的上传按钮,那么您的模板要么没有在template选项中正确引用,要么您的模板未正确呈现在您的构建 Fine Uploader 实例之前的页面。

【讨论】:

  • 我向按钮容器 DIV 添加了一个动态 ID,并在按钮中使用了相同的值:.fineuploader 代码的参数。另外,我也在 text: 参数中添加了按钮的动态名称。最大的问题是我在课程的引号中包含了“qq-hide-dropzone”。我看到了这个,还以为我打错了什么。一旦我确认您在外面有它,我就进行了更改并且它按预期工作。为什么在外面?
【解决方案2】:

这是我目前正在为上传控件的多个实现所做的事情:

我有一些在其他地方列出的 div,其中包含“正在加载”文本(ID:模态框、模态叠加)。它们根据需要上传的文件数量出现,并在它们全部完成后消失。防止用户反复点击,你知道这是怎么回事......

JS:

var fineuploader_@Model.SurveyItemId = $('#files-upload_@Model.SurveyItemId').fineUploader({
        debug: true,
        template: 'qq-template_@Model.SurveyItemId',
        button: $("#uploadButton_@Model.SurveyItemId"),
        request:
        {
            endpoint: '@Url.Action("UploadFile", "Survey")',
            customHeaders: { Accept: 'application/json' },
            params: {
                surveyInstanceId: (function () { return instance_@Model.SurveyItemId; }),
                surveyItemResultId: (function () { return surveyItemResultId_@Model.SurveyItemId; }),
                itemId: (function () { return itemId_@Model.SurveyItemId; }),
                loopingIndex: (function () { return loopingCounter_@Model.SurveyItemId++; })
            }
        },
        validation: {
            acceptFiles: ['image/*', 'application/xls', 'application/pdf', 'text/csv', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'],
            allowedExtensions: ['jpeg', 'jpg', 'gif', 'png', 'bmp', 'csv', 'xls', 'xlsx', 'pdf', 'xlt', 'xltx', 'txt'],
            sizeLimit: 1024 * 1024 * 2.5, // 2.5MB
            stopOnFirstInvalidFile: false
        },
        failedUploadTextDisplay: {
            mode: 'custom'
        },
        multiple: true,
        text: {
            uploadButton_@Model.SurveyItemId: 'Select your upload file(s)'
        }
    }).on('submitted', function (event, id, filename) {
        $("#modal-overlay").fadeIn();
        $("#modal-box").fadeIn();
        filesToUpload_@Model.SurveyItemId++;
        $(':input[type=button], :input[type=submit], :input[type=reset]').attr('disabled', 'disabled');
    }).on('complete', function (event, id, filename, responseJSON) {
        uploadedFileCounter_@Model.SurveyItemId++;
        if (filesToUpload_@Model.SurveyItemId == uploadedFileCounter_@Model.SurveyItemId) {
            $(':input[type=button], :input[type=submit], :input[type=reset]').removeAttr('disabled');
            //$("#overlay").fadeOut();
            $("#modal-box").fadeOut();
            $("#modal-overlay").fadeOut();
        }
    }).on('error', function (event, id, name, errorReason, xhr) {
        //$("#overlay").fadeOut();
        alert('error: ' + errorReason);
        $("#modal-box").fadeOut();
        $("#modal-overlay").fadeOut();
    });
});

HTML: 我向其中的大多数 DIV 添加了动态 ID,但我不确定这是否重要。当我拖放时,所有实例都将显示放置区域,因为悬停会按类更改 div。这对我来说没什么大不了的,但请期待一下。

 <script type="text/template" id="qq-template_@Model.SurveyItemId">
    <div id="container_@Model.SurveyItemId" class="qq-uploader-selector qq-uploader">
        <div id="droparea_@Model.SurveyItemId" class="qq-upload-drop-area-selector qq-upload-drop-area" **qq-hide-dropzone**>
            <span>Drop files here to upload</span>
        </div>
        <div id="uploadButton_@Model.SurveyItemId" class="qq-upload-button-selector qq-upload-button">
            <div>Click here to select your upload file(s)</div>
        </div>
        <span id="processing_@Model.SurveyItemId" class="qq-drop-processing-selector qq-drop-processing">
            <span>Processing dropped files...</span>
            <span id="spinner_@Model.SurveyItemId" class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
        </span>
        <ul id="list_@Model.SurveyItemId" class="qq-upload-list-selector qq-upload-list">
            <li>
                <div id="listprogress_@Model.SurveyItemId" class="qq-progress-bar-container-selector">
                    <div class="qq-progress-bar-selector qq-progress-bar"></div>
                </div>                   
                <span class="qq-upload-file-selector qq-upload-file"></span>
                <span class="qq-upload-status-text-selector qq-upload-status-text"></span>
            </li>
        </ul>
    </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2020-05-14
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多