我的客户使用的是旧的classipress版本,这是我发现的github repo,但他使用的是旧版本。运行最新的Wordpress版本。它带有plupload(一些旧版本),无法在主题中找到该版本。这是Functions.php,这是pluploadHere's the html of my page,无需查看它,但我将其放在那里,因为页面受到了保护,因此,如果需要的话,这是检查整个代码的唯一方法。

我想添加同时上传多张图片的功能,为此,我将this添加到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 hereFilesAdded
    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);
            }
        });
});

09-30 19:13
查看更多