简体   繁体   中英

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

the alert is alert("You Have Reached The MAXIMUM Upload Limit"); so when I add one by one to the specified limit, an alert will appear

<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

$(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. It allows you to configure it as per the need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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