Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Get File Size And Extension Before Uploading Using jQuery

Saturday, Jan 21, 2017

Tags:- jQuery HTML


In this tutorial we will show you how to get file size and file extension before uploading the file using jQuery.It helps in your client side validation you can check size and extension before uploading now there is no need to check in server side it saves time and efficiency of your form.You may also like jQuery Form Validation


See Demo
Get File Size And Extension Before Uploading Using jQuery


To Get File Size And Extension It Takes Only One Step:-

  1. Make a HTML file and define markup, scripting and styling


Step 1.Make a HTML file and define markup, scripting and styling

We make a HTML file and save it with a name get_file_extension.html

<html>
<head>
<script src="jquery.js"></script>
<script>
function get_detail()
{
 var size=$('#file_upload')[0].files[0].size;
 var extension=$('#file_upload').val().replace(/^.*\./, '');
	
 $("#file_detail").html("File Size : "+size+" <br>Extension : "+extension+"");
}
</script>
<style>
body
{
 text-align:center;
 width:100%;
 margin:0 auto;
 padding:0px;
 font-family:helvetica;
 background-color:#58D3F7;
}
#wrapper
{
 text-align:center;
 margin:0 auto;
 padding:0px;
 width:995px;
}
#file_detail
{
 color:#086A87;
}
</style>
</head>
<body>
<div id="wrapper">

<input type="file" id="file_upload" name='file' onchange="get_detail();";>
<p id="file_detail"></p>

</div>
</body>
</html>

In this step we create a file tag to get file size and extension not to upload file we use onchange event and call get_detail() function.In get_detail() function we get the size of the file and to get file extension we use replace function to replace all the string before '.' and that gives the extension of the file and then we display the size and extension.You may also like File Upload With Progress Bar



Thats all, this is how to get file size and extension before uploading using jquery .You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH