简体   繁体   English

限制plupload中的上传次数

[英]Limit the number of uploads in plupload

My client is using an old classipress version, here's a github repo I found but what he's using is much older. 我的客户端使用的是旧的classipress版本,这里是我发现的github repo,但他使用的版本更旧。 Running the latest Wordpress version. 运行最新的Wordpress版本。 It comes with plupload , some old version, couldn't find the version in the theme. 它附带了plupload ,一些旧版本,在主题中找不到版本。 Here's Functions.php , here's plupload . 这是Functions.php ,这是plupload Here's the html of my page, no need to see it, but i'm putting it there because the page is protected so that's the only way to inspect the whole code if you want to. 这是我的页面的html,没有必要看到它,但我把它放在那里因为页面受到保护,所以这是检查整个代码的唯一方法,如果你想。

I want to add the ability to upload multiple pictures at the same time, to do that, I add this to functions.php 我想添加同时上传多张图片的功能, 为此 ,我将其添加到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; }

But I don't know how to stop the user from uploading more than 8 pictures? 但我不知道如何阻止用户上传超过8张图片? I tried adding max_files and max_files_count and max_file_count and nothing worked, I even modified the source code of the plugin itself and the js and nothing worked. 我尝试添加max_filesmax_files_countmax_file_count并没有任何效果,我甚至修改了插件本身和js的源代码,没有任何效果。 I want to stop the user from being able to upload more than 8 images. 我想阻止用户上传超过8张图片。

After I gave up on plupload, I tried doing it using Jquery, again didn't work 在我放弃了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();
     }
 });

Edit 编辑

My pluploadjs here . 我的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();
    });

I modified it to look like so 我修改它看起来像这样

    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();
    });

Is that all? 这就是全部? Will it work now? 它现在有效吗? I haven't tested it because it will give errors 我没有测试它,因为它会给出错误

I'm not sure but your code should almost work. 我不确定,但你的代码几乎可以工作。 I think you should manually remove the files from the queue by calling the removeFile method. 我认为您应该通过调用removeFile方法手动从队列中删除文件。

Maybe try this code: 也许试试这段代码:

    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