Wednesday, June 11, 2014

How to get the File size before uploading using JQuery.

Introduction  :
    Hi, Here i will explain how to code for getting the file size before getting uploaded from the Client Side using Jquery. Here i have used a fileupload control to upload the file and Button to know the size of file in button Click.

Aspx Code :

<title>Get File Size</title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function GetFileSize(fileid) {
var fileSize = 0;
//Checking code for IE
if ($.browser.msie) {
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb
fileSize = fileSize / 1048576; //size in mb
// code for other browsers
else {
fileSize = $("#" + fileid)[0].files[0].size //size in kb
fileSize = fileSize / 1048576; //size in mb
$("#lblSize").text("File Size is : " + fileSize+" MB");
catch (e)
alert("Error is :" + e);
<form name="upload" action="">
<div style="text-align:center">
<input type="file" name="fUpload" id="fUpload" />
<input type="button" value="Get File Size" onclick="GetFileSize('fUpload');" /><br /><br />
<label id="lblSize" style="color:Brown"></label>


No comments:

Post a Comment