Click here to Skip to main content
11,648,951 members (82,373 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Friends,

Can someone help me in getting the size of a file posted in the ASP.Net FileUpload control using javascript..??


Many Thanks,
Hari
Posted 1-Nov-12 20:46pm
hari592309
Comments
ryanb31 at 2-Nov-12 16:39pm
   
As I recall, you can't because JS does not have access to the file system the way you need it to, but you can after it has been uploaded.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This works 100%
Example to get file size before upload using JQuery
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Get File Size</title>
 <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript" > </script>
 <script type="text/javascript">
 function GetFileSize(fileid) {
 try {
 var fileSize = 0;
 //for IE
 if ($.browser.msie) {
 //before making an object of ActiveXObject,
 //please make sure ActiveX is enabled in your IE browser
 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
 }
 //for FF, Safari, Opeara and Others
 else {
 fileSize = $("#" + fileid)[0].files[0].size //size in kb
 fileSize = fileSize / 1048576; //size in mb
 }
 alert("Uploaded File Size is" + fileSize + "MB");
 }
 catch (e) {
 alert("Error is :" + e);
 }
}
 </script>
</head>
<body>
<form name="upload" action="">
<input type="file" name="fUpload" id="fUpload" />
<input type="button" value="Get File Size" onclick="GetFileSize('fUpload');" />
</form>
</body>
</html>
 
Example to get file size before upload in Asp.Net using JQuery
<form id="form1" runat="server">
<asp:FileUpload ID="fUpload" runat="server" />
 <asp:Button ID="btnGetSize" runat="server" Text="Button" OnClientClick="GetFileSize('fUpload');" />
</form>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Get File Size</title>
 <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript" > </script>
 <script type="text/javascript">
 function GetFileSize(fileid) {
 try {
 var fileSize = 0;
 //for IE
 if ($.browser.msie) {
 //before making an object of ActiveXObject,
 //please make sure ActiveX is enabled in your IE browser
 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
 }
 //for FF, Safari, Opeara and Others
 else {
 fileSize = $("#" + fileid)[0].files[0].size //size in kb
 fileSize = fileSize / 1048576; //size in mb
 }
 alert("Uploaded File Size is" + fileSize + "MB");
 }
 catch (e) {
 alert("Error is :" + e);
 }
}
 </script>
</head>
<body>
<form name="upload" action="">
<input type="file" name="fUpload" id="fUpload" />
<input type="button" value="Get File Size" onclick="GetFileSize('fUpload');" />
</form>
</body>
</html>
 
Example to get file size before upload in Asp.Net using JQuery
<form id="form1" runat="server">
<asp:FileUpload ID="fUpload" runat="server" />
 <asp:Button ID="btnGetSize" runat="server" Text="Button" OnClientClick="GetFileSize('fUpload');" />
</form>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If the browser supports html5 you can use the file api

http://www.html5rocks.com/en/tutorials/file/dndfiles/[^]

If the browser doesn't support html5 there isn't really anything you can do.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 F-ES Sitecore 419
1 CPallini 410
2 Kornfeld Eliyahu Peter 330
3 OriginalGriff 324
4 Sergey Alexandrovich Kryukov 254
0 OriginalGriff 1,512
1 Sergey Alexandrovich Kryukov 1,022
2 jyo.net 994
3 DamithSL 986
4 CPallini 945


Advertise | Privacy | Mobile
Web02 | 2.8.150804.4 | Last Updated 22 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100