如何只选择10张而不是多张

时间:2017-07-11 05:46:21

标签: javascript php html html5

如何只选择10张而不是多张 如果弹出更多错误,用户只需上传10张照片

<div class="col-md-12">
    <div class="field" align="left">
    <h3>Upload your images</h3>
    <input type="file" id="files" name="photos[]" multiple />
</div>
<script>

    $(document).ready(function() {
        if (window.File && window.FileList && window.FileReader) {
            $("#files").on("change", function(e) {
                var files = e.target.files,
                    filesLength = files.length;
                for (var i = 0; i < filesLength; i++) {
                    var f = files[i]
                    var fileReader = new FileReader();
                    fileReader.onload = (function(e) {
                        var file = e.target;
                        $("<span class=\"pip\">" +
                            "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" +
                            "<br/><span class=\"remove\">Remove image</span>" +
                            "</span>").insertAfter("#files");
                        $(".remove").click(function() {
                            $(this).parent(".pip").remove();
                        });

                        // Old code here
                        /*$("<img></img>", {
                          class: "imageThumb",
                          src: e.target.result,
                          title: file.name + " | Click to remove"
                        }).insertAfter("#files").click(function(){$(this).remove();});*/

                    });
                    fileReader.readAsDataURL(f);
                }
            });
        } else {
            alert("Your browser doesn't support to File API")
        }
    });
</script>

Route :: get(&#39; image-gallery&#39;,&#39; ImageGalleryController @ index&#39;); Route :: post(&#39; image-gallery&#39;,&#39; ImageGalleryController @ upload&#39;);

3 个答案:

答案 0 :(得分:0)

添加if:

if(files.length > 10){
    //Your code here
}

答案 1 :(得分:0)

您可以将FileList对象传递给Array.prototype.slice()以将FileList转换为Array,将0传递给第一个参数,将10传递给第二个参数参数

&#13;
&#13;
<input type="file" multiple>
<script>
  var input = document.querySelector("input[type=file]");
  input.onchange = function(event) {
    console.log(event.target.files.length);
    var files = Array.prototype.slice.call(event.target.files, 0, 10);
    console.log(files.length);
  }
</script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

只需添加if语句即可检查files.length属性

$(document)
    .ready(function() {
        if (window.File && window.FileList && window.FileReader) {
            $("#files")
                .on("change", function(e) {
                    var files = e.target.files,
                        filesLength = files.length;
                    if (filesLength>10) {
                         alert("You may only select 10 files");
                         $("#files").val(""); 
                         return; 
                         }
                    for (var i = 0; i < filesLength; i++) {
                        var f = files[i]
                        var fileReader = new FileReader();
                        fileReader.onload = (function(e) {
                            var file = e.target;
                            $("<span class=\"pip\">" +
                                    "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" +
                                    "<br/><span class=\"remove\">Remove image</span>" +
                                    "</span>")
                                .insertAfter("#files");
                            $(".remove")
                                .click(function() {
                                    $(this)
                                        .parent(".pip")
                                        .remove();
                                });

                            // Old code here
                            /*$("<img></img>", {
                              class: "imageThumb",
                              src: e.target.result,
                              title: file.name + " | Click to remove"
                            }).insertAfter("#files").click(function(){$(this).remove();});*/

                        });
                        fileReader.readAsDataURL(f);
                    }
                });
        } else {
            alert("Your browser doesn't support to File API")
        }
    });