简体   繁体   English

如何限制上传的图像文件的最大数量

[英]how to limit the maximum number of uploaded image files

I want to limit the maximum upload of image files by adding alerts to the following code 我想通过向以下code添加警报来限制图像文件的最大上载

the alert is alert("You Have Reached The MAXIMUM Upload Limit"); 警报是alert("You Have Reached The MAXIMUM Upload Limit"); so when I add one by one to the specified limit, an alert will appear 所以当我逐个添加到指定的限制时,会出现一个alert

<div class="files col-sm-4" id="files1">
    <input type="file" name="files1" id="imagesix" multiple />
    <br />Selected files:
    <ol class="fileList"></ol>
</div>

script 脚本

$.fn.fileUploader = function (filesToUpload) {
    this.closest(".files").change(function (evt) {

        for (var i = 0; i < evt.target.files.length; i++) {
            filesToUpload.push(evt.target.files[i]);
        };
        var output = [];

        for (var i = 0, f; f = evt.target.files[i]; i++) {
            var removeLink = "<i class=\"fa fa-times fa-close removeFile\" href=\"#\" data-fileid=\"" + i + "\"></i>";

            output.push("<li><strong>", escape(f.name), "</strong> ", removeLink, " </li> ");
        }

        $(this).children(".fileList")
            .append(output.join(""));
    });
};

var filesToUpload = [];

$(document).on("click",".removeFile", function(e){
    e.preventDefault();
    var fileName = $(this).parent().children("strong").text();
    for(i = 0; i < filesToUpload.length; ++ i){
        if(filesToUpload[i].name == fileName){
            filesToUpload.splice(i, 1);
        }   
    }
    $(this).parent().remove();
});


$("#files1").fileUploader(filesToUpload);

this code I implement for my project but as per your requirement you can change it. 这个代码我为我的项目实现,但根据您的要求,您可以更改它。

refrence: this stackoverflow question refrence: 这个stackoverflow问题

$(function(){
  $("input[type='submit']").click(function(){
    var $fileUpload = $("input[type='file']");
    if (parseInt($fileUpload.get(0).files.length)>2){
     alert("You can only upload a maximum of 2 files");
    }
  });    
});

You can use the Dropzone.js plugin for the same. 您可以使用Dropzone.js插件。 It allows you to configure it as per the need. 它允许您根据需要进行配置。

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

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