prevent Upload Large Files .Net

K

Kilic Beg

Hi,

I want to prevent user to upload files larger than 2MB.
If they try to upload I want to redirect them to a error page (ex: File size
larger than 2 MB )..
right now I get a page break "The page cannot be displayed"

I tried to update the Global.asax.cs file this is the code

protected void Application_BeginRequest(Object sender, EventArgs e)
{
long MAX_CONTENTLENGTH = 2097152;
if (Request.ContentLength > MAX_CONTENTLENGTH) {
//Response.Write("File is to big");
Response.Redirecty("Error.html");
}
}

I put the same code also into
protected void Application_Error(Object sender, EventArgs e)
method..

still page break..

where and how can I catch that and redirect to a friendly error page.
do I have to use a third party product for that ?

Please help !!!

Thanks

- Kilic
 
K

Kilic Beg

My problem is not setting the file size limit.

I want to display an error message when the users tries to upload a file
large then in maxRequestLength definied.

right now I have this in my webconfig

<httpRuntime executionTimeout="90" maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" appRequestQueueLimit="100" />

If the users tries to upload files larger than 4MB he will get a page break
"The page cannot be displayed"..
I want to display a friendly page... (ex: saying the file you trying to
uploading is larger than 4MB)

- Kilic
 
R

Roger Helliwell

Hi,

I want to prevent user to upload files larger than 2MB.
If they try to upload I want to redirect them to a error page (ex: File size
larger than 2 MB )..
right now I get a page break "The page cannot be displayed"

Kilic,

Not sure how to check the request size, but if you're using one of the
..NET file upload controls (System.Web.UI.HtmlControls.HtmlInputFile or
2.0 FileUpload) you could simply write

System.Web.UI.HtmlControls.HtmlInputFile File1;

if (File1.PostedFile.ContentLength > MAXFILESIZE) {
Response.Redirect("filetoolarge.aspx");
}
else {
// Save the file on the server
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top