限制plupload中的上传次数

Lyn*_*nob 7 html javascript wordpress jquery 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; }
Run Code Online (Sandbox Code Playgroud)

但我不知道如何阻止用户上传超过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();
     }
 });
Run Code Online (Sandbox Code Playgroud)

编辑

我的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();
    });
Run Code Online (Sandbox Code Playgroud)

我修改它看起来像这样

    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();
    });
Run Code Online (Sandbox Code Playgroud)

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

Fer*_*urg 1

我不确定,但你的代码应该几乎可以工作。我认为您应该通过调用该方法手动从队列中删除文件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);
            }
        });      
});
Run Code Online (Sandbox Code Playgroud)