jQuery Plupload限制上传数量 [英] jQuery Plupload restrict number of uploads

查看:67
本文介绍了jQuery Plupload限制上传数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这段代码上工作了一段时间,试图使其正常工作.我想限制使用上传总共2张以上的图片.

I have been working on this code for some time now trying to get it to work properly. I want to restrict the use from uploading more that 2 images in total.

var upa = $('.uploader').plupload('getUploader');行给出了Uncaught TypeError错误:对象[object Object]没有方法'plupload'

The line var upa = $('.uploader').plupload('getUploader'); it giving an error of 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');
    });

当我上传图像时会产生错误.我不知道我要缺少什么,但是对此的任何帮助将不胜感激.

The error is generated when i upload images. I don't know what I am missing but any help on this is greatly appreciated.

推荐答案

您不清楚要在while (i<=upa.files.length) {块中实现什么.似乎您的页面上有几个上传者,但我不明白这个想法.

What you want to achieve in the while (i<=upa.files.length) { block is not clear to me. Seems like you have several uploaders on your page, but I can't grasp the idea.

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

Anyway, I guess this should do the trick, as to restrict to 2 files max in a single uploader.

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
                    }
                },

希望这会有所帮助

这篇关于jQuery Plupload限制上传数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆