简体   繁体   English

使用Plupload限制上传前可以选择的文件数

[英]Limit the number of files that can be selected before uploading when using Plupload

I'm using Plupload to upload files to my website. 我正在使用Plupload将文件上传到我的网站。 I'm trying to limit the number of files that can be selected before uploading. 我试图限制上传前可以选择的文件数量。 I tried setting multi_selection to false which partially works. 我尝试将multi_selection设置为false ,这部分起作用。 However, if you click on 'Select files' and choose a file via the file explorer, wait for the file explorer to close, then click on 'Select files' and do the same again, you are able to select multiple files. 但是,如果单击“选择文件”并通过文件浏览器选择一个文件,等待文件浏览器关闭,然后单击“选择文件”并再次执行相同操作,则可以选择多个文件。

How can I allow just one file to be selected before uploading? 如何在上传之前只允许选择一个文件?

Here is a demo of the code I'm using: http://jsfiddle.net/5j8c05j9/ 这是我正在使用的代码的演示: http : //jsfiddle.net/5j8c05j9/

Based on the answers in this question: jQuery Plupload restrict number of uploads . 基于此问题的答案: jQuery Plupload限制上传数量

You can add the following property to plupload: 您可以将以下属性添加到plupload中:

max_files: 1,

and then change the FilesAdded function, as such: 然后更改文件FilesAdded功能,如下所示:

FilesAdded: function(up, files) {
    plupload.each(files, function(file) {
        // if there are more files than the allowed  
        if (up.files.length > up.settings.max_files) {
            // display alert message
            alert('Cannot send more than ' + up.settings.max_files + ' file(s).');

            // here you can also hide the "Select Files" button with the following code
            //$(up.settings.browse_button).hide();

            // cancel adding files. break each.
            return false;
        }
        document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
    });
},

Take a look at your updated jsfiddle 看看您更新的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM