关于javascript:plupload中限制上传次数 | 珊瑚贝

Limit the number of uploads in plupload


我的客户使用的是旧的 classipress 版本,这是我找到的 github 存储库,但他使用的是旧版本。运行最新的 WordPress 版本。自带plupload,有些旧版本,在主题里找不到版本。这里是 Functions.php,这里是 plupload。这是我页面的 html,无需查看,但我将其放在那里,因为该页面受到保护,如果您愿意,这是检查整个代码的唯一方法。

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

1
2
3
4
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_files 和 max_files_count 和 max_file_count 并没有任何效果,我什至修改了插件本身和 js 的源代码,但没有任何效果。我想阻止用户上传超过 8 张图片。

放弃plupload后,我尝试使用Jquery,再次失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 /* 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    attachUploader.bind(‘FilesAdded’, function(up, files) {
        jQuery.each(files, function(i, file) {
            jQuery(‘#app-attachment-upload-filelist’).append(
                +
                file.name + ‘ (‘ + plupload.formatSize(file.size) + ‘) ‘ +
                );

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

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

我把它修改成这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    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(
                +
                file.name + ‘ (‘ + plupload.formatSize(file.size) + ‘) ‘ +
                );

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

        up.refresh();

        attachUploader.start();
    });

就这些吗?现在能用吗?我没有测试它,因为它会给出错误

  • 我尝试在 plupload 论坛 plupload.com/punbb/register.php 上询问它说 This forum is not accepting new registrations. Post your questions to StackOverflow instead (with a tag”plupload”). 我也尝试在 github 上询问,我希望得到答案
  • 这可能会有所帮助:stackoverflow.com/a/15520225/1236044
  • @jbl 请阅读我的编辑并告诉我它现在是否可以工作
  • 好吧,在可能刚刚拼接的集合上执行 jQuery.each 似乎不正确。一种测试方法:plupload.com/docs/v2/Create-a-Fiddle
  • @jbl 试过了,没有任何效果,也许 wordpress 中有一些功能。我不知道我放弃了
  • 您的 JQuery.each 应该将 up.files 而不是 files 作为第一个参数。 window.appFileCount += 1; 也可能产生不稳定的行为
  • @jbl我猜是因为给出的答案几乎奏效了


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

也许试试这个代码:

1
2
3
4
5
6
7
8
9
10
    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);
            }
        });      
});

  • 几乎在那里,工作过一次,但我仍然有这个 pastebin.com/04UDCBxY
  • 这是一个堆栈跟踪,因此您应该能够将其追溯到发生这种情况的确切代码行。可能在您使用未定义对象的”id”的地方。确保您不使用已删除的文件对象。
  • 稍后会尝试,我给你赏金,因为它即将到期,我没有时间测试它


来源:https://www.codenong.com/46917999/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8733.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?