1

how to select only 10 photos instead of many User needs to upload only 10 photo if more error popup

<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('image-gallery', 'ImageGalleryController@index'); Route::post('image-gallery', 'ImageGalleryController@upload');

guest271314
  • 1
  • 15
  • 104
  • 177
Veekee45
  • 23
  • 5
  • Possible duplicate of [How to limit maximum items on a multiple input ()](https://stackoverflow.com/questions/10105411/how-to-limit-maximum-items-on-a-multiple-input-input-type-file-multiple) –  Jul 11 '17 at 05:47

3 Answers3

0

Add an if:

if(files.length > 10){
    //Your code here
}
xM0nSt3r
  • 25
  • 1
  • 7
0

You can pass FileList object to Array.prototype.slice() to convert FileList to Array, provide pass 0 to first parameter, 10 to second parameter

<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>
guest271314
  • 1
  • 15
  • 104
  • 177
0

Just add an if statement to check the files.length property

$(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")
        }
    });
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • hi Higgins thanks for your help but preview is not working ..it uploads more than 10 photos..is there any way to solve this..with preview and limits – Veekee45 Jul 11 '17 at 06:13