Click here to Skip to main content
15,120,883 members
Articles / Web Development / ASP.NET
Posted 30 Nov 2005


40 bookmarked

Testing file size before uploading it

Rate me:
Please Sign up or sign in to vote.
2.23/5 (20 votes)
30 Nov 2005
Testing file size before uploading it


there is no way to know file size before  uploading it to your server. uploads file then check its size if its size exceed 10 MB,  yahoo server revokes it in this way you consuming server's memory and bandwidth.that's a bad deal.


you have to write some java script code:

<P>function GetSize(file){</P><P>var fso=new ActiveXObject("Scripting.FileSystemObject");</P><P>var f=fso.getFile(file);</P><P>return fso.getFile(file).size;</P><P>}</P>

this function takes file name and returns file size. it uses windows embed ActiveX to get file size.

then you write another function to do calculations:

function checkFileSize(ctrl){

//check if file size is > 10 MB

if(GetSize(ctrl.value)>10485760)//10 MB=10485760 B


alert('Sorry file size > 10MB');

ctrl.form.reset();//file grater than 10MB then reset form


simply it takes Fileupload control reference and call  GetSize function and compare its result to 10485760(10 MB=10485760 B) if the file size exceed 10 MB,it reset form else do nothing and submit form to use this function  put an html button and add this attribute to it onclick="checkFileSize(this.form.File1);".


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Taha Elsayed
United States United States
Senior Development Manager, with 11 years of experience. Focused on design/developing/delivery B/OSS modules to satisfy customer/business requirements in accordance to TM Forum's TAM/SID framework. Excellent communication and interpersonal skills, flexibility, ability to adjust to new people and situations, strong problem solving skills and willing to work effectively and efficiently. Gained additional leadership skills such as: - High effective mentorship and coaching - Clearly communication and solutions sharing with my team - Decision making

Comments and Discussions

Questionwhat if i dont want to use ActiveXobject Pin
pree_kh29-Nov-12 19:39
Memberpree_kh29-Nov-12 19:39 
GeneralMy vote of 1 Pin
Member 84911541-Jun-12 3:22
MemberMember 84911541-Jun-12 3:22 
GeneralMy vote of 1 Pin
Andrew Nefedkin29-Mar-10 6:27
MemberAndrew Nefedkin29-Mar-10 6:27 
GeneralMy vote of 1 Pin
HarishBhattbhatt22-Sep-09 19:10
MemberHarishBhattbhatt22-Sep-09 19:10 
GeneralVery poor example Pin
GaryWoodfine 15-Oct-08 12:42
professionalGaryWoodfine 15-Oct-08 12:42 
GeneralHelp Pin
pradeep_puru6-Sep-07 20:14
Memberpradeep_puru6-Sep-07 20:14 
GeneralNot the solution at all. Pin
ChrisAdams2-Dec-05 11:21
MemberChrisAdams2-Dec-05 11:21 
GeneralPart of the solution Pin
steve_hocking30-Nov-05 6:35
Membersteve_hocking30-Nov-05 6:35 
GeneralRe: Part of the solution Pin
Taha Elsayed30-Nov-05 6:46
MemberTaha Elsayed30-Nov-05 6:46 
GeneralRe: Part of the solution Pin
DevDude30-Nov-05 6:57
MemberDevDude30-Nov-05 6:57 
GeneralRe: Part of the solution Pin
Taha Elsayed30-Nov-05 7:01
MemberTaha Elsayed30-Nov-05 7:01 
GeneralRe: Part of the solution Pin
DevDude30-Nov-05 7:08
MemberDevDude30-Nov-05 7:08 
GeneralRe: Part of the solution Pin
louisa.040230-Nov-05 7:17
Memberlouisa.040230-Nov-05 7:17 
GeneralRe: Part of the solution Pin
DevDude30-Nov-05 8:05
MemberDevDude30-Nov-05 8:05 
GeneralRe: Part of the solution Pin
Taha Elsayed30-Nov-05 20:30
MemberTaha Elsayed30-Nov-05 20:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.