Well, the file size that .size returns is 1024 characters, it will be 0.102 MB. But anyway:
$(document).ready(function() { $('input[type="file"]').change(function(event) { var totalBytes = this.files[0].size; if(totalBytes < 1000000){ var _size = Math.floor(totalBytes/1000) + 'KB'; alert(_size); }else{ var _size = Math.floor(totalBytes/1000000) + 'MB'; alert(_size); } }); });
Keep in mind that I wrote that it does not check basic cases, this means that if the file is under 1000 bytes, then you will get 0 KB (even if it's something like 435 bytes). Also, if your file is GB in size, it just warns of something like 2300MB. I also get rid of the decimal, so if you want something like 2.3MB, then do not use Floor.
source share