Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  139] [ 1]  / answers: 1 / hits: 37035  / 9 Years ago, wed, november 4, 2015, 12:00:00

I want get the size of file in MB if the value is > 1024 or in KB if < 1024



$(document).ready(function() {
$('input[type=file]').change(function(event) {
var _size = this.files[0].size + 'mb';
alert(_size);
});
});

<input type=file>

More From » jquery

 Answers
11

Please find the updated code below. See the working example here, Cheers!




 (function() {
document.querySelector('input[type=file]').addEventListener(change, function(event) {
var _size = this.files[0].size;
var fSExt = new Array('Bytes', 'KB', 'MB', 'GB'),
i=0;while(_size>900){_size/=1024;i++;}
var exactSize = (Math.round(_size*100)/100)+' '+fSExt[i];
console.log('FILE SIZE = ',exactSize);
alert(exactSize);
});
})();

<input type=file />




[#64499] Tuesday, November 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donovanjasek

Total Points: 465
Total Questions: 103
Total Answers: 113

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
;