0

I wanna get Width and height of the Input file image before update. Its better to get in onchange. but i cant get that actual width and height.

this is cshtml code:

here "imgIdentityPicture" is image view box

<div class="col-md-2 col-md-offset-1">
                            <input type="file" name="IdentityPicture" id="IdentityPicture" />
                        </div>

this is script on change :

$(function () {
    // Save Customer Identity
    $('#IdentityPicture').on('change', function (e) {
        e.preventDefault();
        var form = $(this).parents('form:first');

        $('#imgIdentityPicture').attr("src", '/Content/Images/Animation/loading_fast.gif');

        form.ajaxSubmit({
            success: function (data) {
                $('#imgIdentityPicture').attr("src", data.Url);
                $('#SettleBeneficiaryIdPicture').val(data.FileName);

            },
            beforeSubmit: function (arr, $form, options) {

            },
            error: function () {
                alert('error');
            }

        });
    });
});
tereško
  • 58,060
  • 25
  • 98
  • 150
user2710638
  • 115
  • 1
  • 3
  • 15

2 Answers2

1
       <input type="file" name="photo" id="photoInput" />

 $.validator.addMethod('imagedim', function(value, element, param) {
    var _URL = window.URL;
    var  img;
    if ((element = this.files[0])) {
        img = new Image();
        img.onload = function () {
            console.log("Width:" + this.width + "   Height: " + this.height);//this will give you image width and height and you can easily validate here....

            return this.width >= param
        };
        img.src = _URL.createObjectURL(element);
    }
 });

See this.. (How to Preview Image, get file size, image height and width before upload?)

Community
  • 1
  • 1
Anand G
  • 3,130
  • 1
  • 22
  • 28
0

Try this,

$(function(){
    $('#IdentityPicture').on('change', function (e) {
        e.preventDefault();
        var form = $(this).parents('form:first');

        $('#imgIdentityPicture').attr("src", '/Content/Images/Animation/loading_fast.gif');

        /* code to get width of image */
        var file, img;
        if ((file = this.files[0])) {
           img = new Image();
           img.onload = function () {
               alert(this.width + " " + this.height);
           };
        }
        form.ajaxSubmit({
            success: function (data) {
                $('#imgIdentityPicture').attr("src", data.Url);
                $('#SettleBeneficiaryIdPicture').val(data.FileName);

            },
            beforeSubmit: function (arr, $form, options) {

            },
            error: function () {
                alert('error');
            }
        });
    });
});
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106