How to get file Information like name, size, type and modified date in JavaScript

It is great idea to apply client side validation to make form or file validation fast, Always do validate file information before sending to sever like if user upload some file, apply client side validation on file type, size which help user to upload valid file on server, Following are the single line of code is JavaScript to get name, size, type and last modified date of selected files.

Get File Name in JavaScript

var fileName = document.getElementById('file').files[0].name;
console.log(fileName);
alert(fileName);



Get File Type in JavaScript

var fileType = document.getElementById('file').files[0].type;
console.log(fileType);
alert(fileType);

Get File Size in JavaScript

var fileSize = document.getElementById('file').files[0].size;
console.log(fileSize);
alert(fileSize);

Get File Lst Modified Date in JavaScript

var fileLastModifiedDate = document.getElementById('file').files[0].lastModifiedDate;
console.log(fileLastModifiedDate);
alert(fileLastModifiedDate);



Using above function you can easily create custom JavaScript function to get all the file details in array

function getFileInfo(){
    var fileInfo = new array();
        fileInfo['fileName'] = document.getElementById('file').files[0].name;
        fileInfo['fileSize'] = document.getElementById('file').files[0].size;
        fileInfo['fileType'] = document.getElementById('file').files[0].type;
        fileInfo['fileModifiedDate'] = document.getElementById('file').files[0].lastModifiedDate;
        console.log(fileInfo);
}



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.