Answer the question
In order to leave comments, you need to log in
How to properly check image resolution with jQuery before uploading?
Hello, the project on Asp.Net MVC is used and there is a form on which there is input type="file" on which the rule is hung. The task is to immediately after we have selected the image, check it for height and width. Here is the code
<input type="file" name="FacebookImage" class="input-width-hundred-percent" data-rule-imageFacebookResolution="true" id="FacebookImage" />
$.validator.addMethod("data-rule-imageFacebookResolution", function (value, elem, attrValue) {
var _URL = window.URL;
var file, img;
if ((file = elem.files[0])) {
img = new Image();
img.onload = function () {
alert("Width:" + this.width + " Height: " + this.height);
if (this.width < 520 && this.height < 290) {
return false;
}
return true;
};
img.src = _URL.createObjectURL(file);
}
return false;
}, "Минимальное разрешение должно быть 520 x 290);
$("#SaveBtn").click(function () {
var form = $("#ettings");
form.validate();
if (form.valid()) {
var data = new FormData(document.getElementById('settings'));
$.ajax({
url: '@Url.Action(MVC.Product.SocialShareEdit())',
type: "POST",
data: data,
processData: false,
contentType: false,
success: function () {
$("#modal").modal('hide');
$.ajax({
url: '@Url.Action(MVC.Product.SocialShareIconsPartial(Model.DeliveryType))',
type: "POST",
processData: false,
contentType: false,
success: function (message) {
$("#Icons").html(message);
}
});
}
});
}
});
Answer the question
In order to leave comments, you need to log in
I would do so. I would hang an onchange handler on the input file, determine the width and height of the image in it, and put this matter in the dataset, and in the filter I would already check from the dataset values.
$('#FacebookImage').on('change',function(){
var elem = this;
var _URL = window.URL;
var file, img;
if ((file = elem.files[0])) {
img = new Image();
img.onload = function () {
elem.dataset['imageWidth'] = this.width;
elem.dataset['imageHeight'] = this.height;
};
img.src = _URL.createObjectURL(file);
}
});
$.validator.addMethod("data-rule-imageFacebookResolution", function (value, elem, attrValue) {
var width = parseInt(elem.dataset['width']);
var height = parseInt(elem.dataset['height']);
if width < 520 && height < 290) {
return false;
}
return true;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question