Get Image Size in jquery or javascript
<!DOCTYPE html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
$('#divImageSize').find('#fileUpload').change(function () {
CalculateImageSize();
})
})
function CalculateImageSize() {
var
imgSize = ($('#divImageSize').find("input[name='fileUpload']")[0].files[0].size / 1024);
var
actualByteSize = imgSize;
if (imgSize / 1024 > 1) {
if (((imgSize / 1024) / 1024) > 1) {
imgSize =
(Math.round(((imgSize / 1024) / 1024) * 100) / 100);
$('#divImageSize').find("#lblSize").html("file size is:- " + imgSize + "Gb");
}
else {
imgSize =
(Math.round((imgSize / 1024) * 100) / 100)
$('#divImageSize').find("#lblSize").html("file size is:- " + imgSize + "Mb");
}
}
else
{
imgSize = (Math.round(imgSize *
100) / 100)
$('#divImageSize').find("#lblSize").html("file size is:- " + imgSize + "kb");
}
if
(parseInt(actualByteSize) > 4000) {
alert("Image size should not
be more than 4 MB");
}
}
</script>
</head>
<body>
<div id="divImageSize">
<label>Please select file :- </label>
<input type="file" id="fileUpload" name="fileUpload" />
<label id="lblSize" style="color: red; font-weight: bold"></label>
</div>
</body>
</html>
Output
Explanation Image Size Jquery
Its important because some where we have to set validation if image or file size greater than 4 MB then popup a alert .Hope this code will help you. If any issue then let me ask . You can get image size with only javascript scripting language.
0 comments:
Post a comment