【问题标题】:jQuery Plupload restrict number of uploadsjQuery Plupload 限制上传次数
【发布时间】:2013-10-03 17:55:39
【问题描述】:

我已经在这段代码上工作了一段时间,现在试图让它正常工作。我想限制使用总共上传超过 2 张图片。

var upa = $('.uploader').plupload('getUploader'); 行给出了 Uncaught TypeError: Object [object Object] has no method 'plupload' 的错误

    var maxfiles=2;
    $('.uploader').each(function(){
        var $uploader = $(this);
        $uploader.pluploadQueue($.extend({
            runtimes: 'html5,flash,html4',
            url : '../admin/extras/upload.php',
            max_file_size : '2mb',
            chunk_size : '2mb',
            unique_names : true,                
            filters : [
                {title : "Image files", extensions : "jpg"}
            ],
            resize : {width : 800, height : 600, quality : 90},
            flash_swf_url : 'js/mylibs/forms/uploader/plupload.flash.swf',
            init : {
                FilesAdded: function(up, files) {
                    plupload.each(files, function(file) {
                        if (up.files.length > maxfiles) {
                            up.removeFile(file);
                        }
                        var upa = $('.uploader').plupload('getUploader');
                        var i = 0;
                        while (i<=upa.files.length) {
                            ultimo = upa.files.length;
                            if (ultimo > 1) {
                                if (i > 0) {
                                    ultimo2 = ultimo - 1;
                                    ii = i-1;
                                    if (ultimo2 != ii) {
                                        if (up.files[ultimo - 1].name == upa.files[i-1].name) {
                                            up.removeFile(file);
                                        }
                                    }
                                }
                            }
                            i++;
                        }
                    });
                    if (up.files.length >= maxfiles) {
                        $('#uploader_browse').hide("slow");
                    }
                },
                FilesRemoved: function(up, files) {
                    if (up.files.length < maxfiles) {
                        $('#uploader_browse').fadeIn("slow");
                    }
                }
            }
        }));
        $uploader.find('.plupload_button').addClass('button grey btn');
        $uploader.find('.plupload_add').addClass('icon-plus');
        $uploader.find('.plupload_start').addClass('icon-ok');
    });

我上传图片时产生错误。我不知道我错过了什么,但非常感谢任何帮助。

【问题讨论】:

    标签: jquery plupload


    【解决方案1】:

    我不清楚你想在while (i&lt;=upa.files.length) { 块中实现什么。您的页面上似乎有多个上传者,但我无法理解这个想法。

    无论如何,我想这应该可以解决问题,因为在单个上传器中最多限制为 2 个文件。

    FilesAdded: function(up, files) {
                        var maxfiles = 2;
                        if(up.files.length > maxfiles )
                         {
                            up.splice(maxfiles);
                            alert('no more than '+maxfiles + ' file(s)');
                         }
                        if (up.files.length === maxfiles) {
                            $('#uploader_browse').hide("slow"); // provided there is only one #uploader_browse on page
                        }
                    },
    

    希望这会有所帮助

    【讨论】:

    • 我是 php 人而不是 JavaScript 人,我刚开始使用 js 和 jQuery。我从他们的论坛上得到了代码。此编辑非常感谢您的意见。
    【解决方案2】:

    好答案jbl。 我稍微调整了您的解决方案,使其更通用,并在需要时重新出现“添加文件”按钮。

    uploader.bind('FilesAdded', function(up, files) {
      if (up.files.length >= up.settings.max_files) {
        up.splice(up.settings.max_files);
        $(up.settings.browse_button).hide();
      }
    });
    
    uploader.bind('FilesRemoved', function(up, files) {
      if (up.files.length < up.settings.max_files) {
        $(up.settings.browse_button).show();
      }
    });
    

    max_files 是 pluploadQueue 设置的一部分

    $("#uploadBox").pluploadQueue({
      ...
      max_files: 2,
      ...
    });
    

    【讨论】:

    • up.settings.browse_button 包含浏览按钮的 ID。我必须添加一个 id-selector 才能使其工作。所以,使用$('#' + up.settings.browse_button).hide();
    猜你喜欢
    • 2018-04-05
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2014-10-23
    相关资源
    最近更新 更多