All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get File Size And Extension Before Uploading Using jQuery

Last Updated : Jul 1, 2023

IN - jQuery HTML | Written & Updated By - Ashish

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

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.

I hope this tutorial on get file size using jquery and php helps you and the steps and method mentioned above are easy to follow and implement.