繁体   English   中英

限制plupload中的上传次数

[英]Limit the number of uploads in plupload

我的客户端使用的是旧的classipress版本,这里是我发现的github repo,但他使用的版本更旧。 运行最新的Wordpress版本。 它附带了plupload ,一些旧版本,在主题中找不到版本。 这是Functions.php ,这是plupload 这是我的页面的html,没有必要看到它,但我把它放在那里因为页面受到保护,所以这是检查整个代码的唯一方法,如果你想。

我想添加同时上传多张图片的功能, 为此 ,我将其添加到functions.php

add_filter('appthemes_plupload_config', 'enable_plupload_multisel', 10 ,1);
function enable_plupload_multisel($app_plupload_config){
$app_plupload_config['plupload']['multi_selection'] = true;
return $app_plupload_config; }

但我不知道如何阻止用户上传超过8张图片? 我尝试添加max_filesmax_files_countmax_file_count并没有任何效果,我甚至修改了插件本身和js的源代码,没有任何效果。 我想阻止用户上传超过8张图片。

在我放弃了plupload后,我尝试使用Jquery,再次没有工作

 /* prevent form submission if user selects more than 8 pics */
 jQuery('#app-attachment-upload-pickfiles').change(function() {
     if (this.files.length > 8) {
         alert('Uploading more than 8 images is not allowed');
         this.value = '';
     }
 });
 // Prevent submission if limit is exceeded.
 jQuery('#mainform').submit(function() {
     if (this.files.length > 8) {
         jQuery('#app-attachment-upload-pickfiles').hide();
         jQuery('#step1').hide();
         return false;
     } else {
         jQuery('#app-attachment-upload-pickfiles').show();
         jQuery('#step1').show();
     }
 });

编辑

我的pluploadjs在这里 FilesAdded

    attachUploader.bind('FilesAdded', function(up, files) {
        jQuery.each(files, function(i, file) {
            jQuery('#app-attachment-upload-filelist').append(
                '<div id="' + file.id + '" class="app-attachment-upload-progress">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');

            window.appFileCount += 1;
            APP_Attachment.hideUploadBtn();
        });

        up.refresh();
        attachUploader.start();
    });

我修改它看起来像这样

    attachUploader.bind('FilesAdded', function(up, files) {
        var maxfiles = 8;
            if(up.files.length > maxfiles )
             {
                up.splice(maxfiles);
                alert('no more than '+maxfiles + ' file(s)');
             }
            if (up.files.length === maxfiles) {
                $('#app-attachment-upload-filelist').hide("slow"); // provided there is only one #uploader_browse on page
            }
        jQuery.each(files, function(i, file) {
            jQuery('#app-attachment-upload-filelist').append(
                '<div id="' + file.id + '" class="app-attachment-upload-progress">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
                '</div>');

            window.appFileCount += 1;
            APP_Attachment.hideUploadBtn();
        });

        up.refresh();

        attachUploader.start();
    });

这就是全部? 它现在有效吗? 我没有测试它,因为它会给出错误

我不确定,但你的代码几乎可以工作。 我认为您应该通过调用removeFile方法手动从队列中删除文件。

也许试试这段代码:

    attachUploader.bind('FilesAdded', function(up, files) {
        var maxfiles = 8;

        // remove all new files after the max of files
        jQuery.each(up.files, function(i, file) {
            if(i > maxfiles){
                up.removeFile(file);
            }
        });      
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM