jQuery Plupload restrict number of uploads

戏子无情 提交于 2019-11-27 22:25:06

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.

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

Hope this will help

Good answer jbl. I tweaked your solution a bit to make it more generic, and to make the 'Add files' button reappear when needed.

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 is part of the pluploadQueue settings

$("#uploadBox").pluploadQueue({
  ...
  max_files: 2,
  ...
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!