Monday, 26 November 2018

Get input type file size in KB


HTML

<input type="file" class="upload" onchange="readURL(this)" id="file" accept="image/x-
png,image/jpeg" />

<input type="button" value="Upload" class="btn btn-default" style="margin:auto;display:block" onclick="checksize(this)" />

Java Script

Get File Name in JavaScript:
document.getElementById('file').files[0].name
Get File Size in JavaScript:

document.getElementById('file').files[0].size
Get File Type in JavaScript:
document.getElementById('file').files[0].type
Get File Modified Date in JavaScript:
document.getElementById('file').files[0].lastModifiedDate
You can use the following code to get the file information after selecting a file.

Get file extension


var fname=document.getElementById('File_name').files[0].name;
var ext=fname.split('.').pop();

Jquery


// check file size
function checksize(file) {
  
    var size = $j("#" + file)[0].files[0].size / 1024;
   
    if (size>100) {
       alert("File size exceed. Maximum 100kb is allowed.");
        return false;
    }
    else {
       alert("ok");
        return true;
    }
   
}


No comments:

Post a Comment