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;

Get File Type in JavaScript

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

Get File Size in JavaScript

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

Get File Lst Modified Date in JavaScript

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

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;

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.