0

I want to have input tag for uploading multiple images.

I have format limitation in preview (called on change trigger) as below. How can I add another limitation for images greater than 100kb?

function previewPhotoInput() {
    var $preview = $('#previewPhoto');
    if (this.files) {
        var files = this.files;
        $.each(files, function (i, file) {
            if (!/\.(jpeg|jpg)$/i.test(file.name)) {
                return alert("Invalid Format!");
            } 
            var reader = new FileReader();
            $(reader).on("load", function () {
                $preview.append($("<img/>", { src: this.result, height: 100 }));
            });
            reader.readAsDataURL(file);
        });
    }
}
Elnaz
  • 2,854
  • 3
  • 29
  • 41

4 Answers4

2

You can use file.size, on the input change event to get the size in bytes. Based on that you could provide an alert regarding the file size:

if(file.size > 100000){
    return alert("file is to big")
}

Demo

function previewPhotoInput(obj) {
  var $preview = $('#previewPhoto');
  if (obj.files) {
    var files = obj.files;
    $.each(files, function(i, file) {
      if (!/\.(jpeg|jpg)$/i.test(file.name)) {
        return alert("Invalid Format!");
      }
      if(file.size > 100000){
        return alert("file is to big")
      }
      var reader = new FileReader();
      $(reader).on("load", function() {
        $preview.append($("<img/>", {
          src: this.result,
          height: 100
        }));
      });
      reader.readAsDataURL(file);
    });
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" multiple onchange="previewPhotoInput(this)">
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
0

Add data-max-size="100" in your input tag

  <form  class="upload-form">
        <input id="previewPhoto" class="upload-file" data-max-size="100" type="file" >
        <input type=submit>
    </form>

Jquery invoking function : on submit button this function will be execute. It will get Input tag's max-size attribute value and compare it with Files size value.

$(function(){
    var fileInput = $('#previewPhoto');
    var maxSize = fileInput.data('max-size');
    $('#previewPhoto').submit(function(e){
        if(fileInput.get(0).files.length){
            var fileSize = fileInput.get(0).files[0].size; // in bytes
            if(fileSize>maxSize){
                alert('file size is more then' + maxSize + ' bytes');
                return false;
            }else{
                alert('file size is correct- '+fileSize+' bytes');
            }
        }else{
            alert('choose file, please');
            return false;
        }

    });
});
Pedram
  • 15,766
  • 10
  • 44
  • 73
Saurin
  • 1,650
  • 1
  • 10
  • 8
0

Use file.size . You will size in bytes but you can convert to KB.

     function previewPhotoInput() {
            var $preview = $('#previewPhoto');
            if (this.files) {
                var files = this.files;

                $.each(files, function (i, file) {
                       var size=(file.size)/1000;
                       if(size>100){
                          return alert("File should be less than 100K"); 
                       }

                       if (!/\.(jpeg|jpg)$/i.test(file.name)) {
                          return alert("Invalid Format!");
                       } 

                    var reader = new FileReader();
                    $(reader).on("load", function () {
                        $preview.append($("<img/>", { src: this.result, height: 100 }));
                    });
                    reader.readAsDataURL(file);
                });
            }
        }
Elnaz
  • 2,854
  • 3
  • 29
  • 41
Yahya Mukhtar
  • 474
  • 5
  • 13
  • Thanks, I moved var size = (file.size) / 1000; if (size > 100) { return alert("err msg!"); } in each loop, since I had error on files[0]. please edit your answer. I will accept then. – Elnaz Nov 08 '17 at 06:38
0

Use following method and call while submitting your input form or while file input control changes

function ValidateImageFile() {
                    var file = $(".fupFileUpload")[0].files[0]; //fupFileUpload add class to your file upload control
                    var fileType = file.type;
                    var ValidImageTypes = ["image/gif", "image/jpeg", "image/png", "image/tiff", , "image/bmp"];
                    if ($.inArray(fileType, ValidImageTypes) < 0) {
                       alert("Please select valid image file."); //set label text if you want to display error message in label
                        return false;
                    }
                    else if( file.size > 1000000){ // file.size is in bytes
                        alert("Please select file less than 100 kb.");
                        return false;
                    }
                    return true;
                }