3

这是代码的一部分:

inprogress = false;

function getid(id) {
    return document.getElementById(id); 
}

var uploader = new plupload.Uploader({
    runtimes : 'gears,html5,flash,silverlight,browserplus',
    browse_button : 'link-browse',
    max_file_size : '100mb',
    url : 'site/upload/process.php?dir=<?php echo $uplid; ?>',
    flash_swf_url : 'site/upload/plupload.flash.swf',
    silverlight_xap_url : 'site/upload/plupload.silverlight.xap',
});
uploader.bind('Init', function(up, params) {
    //$('filelist').innerHTML = "<div>Current runtime: " + params.runtime + "</div>";
});
uploader.bind('FilesAdded', function(up, files) {
    if(uploader.files.length <= 0){
        var element = document.getElementById('standby');
        element.parentNode.removeChild(element);
    }
    if(up.files.length > 4 || uploader.files.length > 4)
    {
        alert('Only 5 files per upload !');

        return false;
    }
    for (var i in files) {
        getid('filelist').innerHTML += '<div class="item" id="' + files[i].id + '"><div class="name">' + files[i].name + '</div><div onclick="removeme(\''+files[i].id+'\')" id="remove-'+files[i].id+'" class="remove"></div><div class="size">[ ' + plupload.formatSize(files[i].size) + ' ]</div><div class="percent"></div></div>';
    }
});
uploader.bind('UploadFile', function(up, file) {
    getid('submit-form').innerHTML += '<input type="hidden" name="file-' + file.id + '" value="' + file.name + '" />';
});
uploader.bind('UploadProgress', function(up, file) {
    getid(file.id).getElementsByTagName('div')[3].innerHTML = '<span>' + file.percent + "%</span>";
});
uploader.bind('StateChanged', function(uploader) {
        if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
         window.location = "./dl/<?php echo $uplid; ?>"
        }
    });
getid('link-upload').onclick = function() {
    if(uploader.files.length < 1){
        alert('Please select files first.');
        return false;
    }
    inprogress = true;
    uploader.start();
    return false;
};
uploader.init();
function removeme(id){
    if(inprogress) return false;
    if(uploader.files.length == 1)
    getid('filelist').innerHTML += '<div id="standby"></div>';

    var element = document.getElementById(id);
    element.parentNode.removeChild(element);

    var toremove = '';

    for(var i in uploader.files){
        if(uploader.files[i].id === id){
            toremove = i;
        }
    }
    uploader.files.splice(toremove, 1);
}

我可以限制上传的文件,如果我选择了 4 个文件,我又选择了 5 个 -> 它将显示错误

但如果我首先选择例如 14 个文件,它们将显示在“文件列表”中。

如何限制,或者在哪里放置“return false”;

谢谢你的帮助 :)

4

4 回答 4

5

- Working for Pupload v2.0

$("#uploader").pluploadQueue({
    runtimes: 'html5,html4',
    url: 'upload.php',
    max_file_size: '2mb',
    unique_names: false,
    rename: true,
    prevent_duplicates: true,
    init : {
        FilesAdded: function(up, files) {
          var max_files = 12;
          plupload.each(files, function(file) {
            if (up.files.length > max_files) {
              alert('You are allowed to add only ' + max_files + ' files.');
              up.removeFile(file);
            }
          });
          if (up.files.length >= max_files) {
            $('#pickfiles').hide('slow');
          }
        },
        FilesRemoved: function(up, files) {
          if (up.files.length < max_files) {
            $('#pickfiles').fadeIn('slow');
          }
        }
      },
    resize : {width : 700, height : 700, quality : 90},
    filters: [
        {
            title: "Image files",
            extensions: "jpg,jpeg,gif,png"
        }
    ]
});
于 2013-10-21T01:09:47.467 回答
3

展开if(up.files.length > 4 || uploader.files.length > 4)if(up.files.length > 4 || uploader.files.length > 4 || files.length > 4)

于 2011-09-12T21:06:39.130 回答
1

只需使用 max_file_count: 4 选项来限制要上传的文件数量

于 2012-05-08T07:23:55.290 回答
0

回复 Rob W 的回答,up并且uploader是相同的,因为它是上传者实例,因此是多余的;(uploader.files.length + files.length) > 4当考虑到已经“注册”的文件时(例如,有人添加了 2 个文件,然后添加了 3 个文件),还可以检查以检查传入的文件是否超过 4 个。

所以总而言之,我会推荐

if(uploader.files.length > 4 || files.length > 4 || (uploader.files.length + files.length) > 4) {
于 2013-05-03T12:26:12.840 回答