I used the following methode
HTML
<input type="file" id="loadfile" />
JavaScript
var file = document.getElementById('loadfile').files[0];
alert( "name " + file.name + "Size " + file.size );
It works fine other browsers except IE :( How to get in IE ?
The id="size" element will be used to display the size of selected file from the id="upload" element. We listen on the change event of the file input, and get the selected files via e.
Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).
Javascript – The File Size Is Measured In Bytes With Code Examples.
IE doesn't supply file size information I'm afraid. You might be able to use HTML5 File API with IE10, see here:-
Javascript to check filesize before upload in Internet Explorer
you can do it like this using activeX
function getSize()
{
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");
}
see here for more detail;
how validate file size using HTML and Javascript on client side
document.getElementById('loadfile').addEventListener('change', checkFile, false);
function checkFile(e) {
var file_list = e.target.files;
for (var i = 0, file; file = file_list[i]; i++) {
var fileExtension = file.name.split('.')[file.name.split('.').length - 1].toLowerCase();
var iConvert = (file.size / 1024).toFixed(2);
txt = "File type : " +fileExtension + "\n";
if(file.size > (1024 * 1024)){
txt += "Size: " + (file.size / (1024*1024)).toFixed(2) + " MB \n";
} else {
txt += "Size: " + (file.size / 1024).toFixed(2) + " KB \n";
}
alert(txt);
}
}
see filddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With