2

I use this code for check file type

// - coursesweb.net
  // get the file name and split it to separe the extension
  var name = el.value;
  var ar_name = name.split('.');

but now i want to apply this code to get file size , how can i do ?

code from this site

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
user2979339
  • 33
  • 1
  • 1
  • 5

4 Answers4

11
You can check the filesize by files[0].size

try the following:

$('#myFile').bind('change', function() {

  //this.files[0].size gets the size of your file.
  alert(this.files[0].size);

});
uhs
  • 838
  • 1
  • 7
  • 21
  • 1
    +1 upvoted as this is a legitimate answer and the downvote without a comment is unacceptable – hammus Nov 22 '13 at 04:59
5

try this to get file size

document.getElementById('fileid').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);
    }
}

fiddle

Harish Singh
  • 3,359
  • 5
  • 24
  • 39
2

If browser supports File API.

if (typeof FileReader !== "undefined") {
    var size = document.getElementById('file_input_field_id').files[0].size;
} else {
    console.log("File API is not supported");
    var size = -1;
}

If you want to get file size on client side w/o File API, needs to use flash. You can use lib like FileAPI.js for this

Max Lazar
  • 121
  • 5
1

You can check the file size like this.

Lets say your HTML is:

<input type="file" id="file" />

 var size = document.getElementById('file').files[0].size;
Engineer
  • 5,911
  • 4
  • 31
  • 58