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

Stats

92.6K views
1.3K downloads
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

Abstract

there is no way to know file size before  uploading it to your server. http://www.yahoomail.com/ 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.

Solution

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 server.to use this function  put an html button and add this attribute to it onclick="checkFileSize(this.form.File1);".
 

License

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


Written By
Architect http://www.integrant.com/
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 18:39
pree_kh29-Nov-12 18:39 
GeneralMy vote of 1 Pin
Member 84911541-Jun-12 2:22
Member 84911541-Jun-12 2:22 
GeneralMy vote of 1 Pin
Andrew Nefedkin29-Mar-10 5:27
Andrew Nefedkin29-Mar-10 5:27 
GeneralMy vote of 1 Pin
HarishBhattbhatt22-Sep-09 18:10
HarishBhattbhatt22-Sep-09 18:10 
GeneralVery poor example Pin
GaryWoodfine 15-Oct-08 11:42
professionalGaryWoodfine 15-Oct-08 11:42 
GeneralHelp Pin
pradeep_puru6-Sep-07 19:14
pradeep_puru6-Sep-07 19:14 
GeneralNot the solution at all. Pin
ChrisAdams2-Dec-05 10:21
ChrisAdams2-Dec-05 10:21 
GeneralPart of the solution Pin
steve_hocking30-Nov-05 5:35
steve_hocking30-Nov-05 5:35 
GeneralRe: Part of the solution Pin
Taha Elsayed30-Nov-05 5:46
Taha Elsayed30-Nov-05 5:46 
GeneralRe: Part of the solution Pin
DevDude30-Nov-05 5:57
DevDude30-Nov-05 5:57 
GeneralRe: Part of the solution Pin
Taha Elsayed30-Nov-05 6:01
Taha Elsayed30-Nov-05 6:01 
GeneralRe: Part of the solution Pin
DevDude30-Nov-05 6:08
DevDude30-Nov-05 6:08 
Your only addressing half the issue. You can't control the environment in a public web application. Thefore validators address this solution by running on the server for downlevel browsers. Your file upload solution will only work for IE, running JS, and allowing access to the local disk. However you could still use this code and just detect the browser first and don't run it if IE and JS is not supported. But to address your statement which is incorrect, validators do still work if JS is diabled, thats the beauty of them. They work in most *all* cases and the programmer is oblivient to this, it just works. Even with the case of custom validators, you just add your own server side implmentation and your good to go if you run into the situation where JS is disabled or its labled as a down level browser. BTW, we use file upload from softartisans.com that handles file uploads more efficiently then asp.net default way of handling them.
GeneralRe: Part of the solution Pin
louisa.040230-Nov-05 6:17
louisa.040230-Nov-05 6:17 
GeneralRe: Part of the solution Pin
DevDude30-Nov-05 7:05
DevDude30-Nov-05 7:05 
GeneralRe: Part of the solution Pin
Taha Elsayed30-Nov-05 19:30
Taha Elsayed30-Nov-05 19: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.