Uploading large files in ASP.NET 2.0 gives a DNS Error
Posted on 8/18/2006 10:09:32 AM
in #ASP.NET 2.X
File upload has some big problem in asp.net 2.0 If you try
to upload some file of more than 4 mb you will get and error. This is the
default file size limit of dot net. This is defined in Machine.config. You can
overwrite this in web.config. Like this
<System.Web> <httpRuntime maxRequestLength="10000"
/> </System.Web>
The number “10000” is in Kilobytes. web.config only deals
with the overall size of the entire request. But with this configuration in
place if you try to upload a file of lets say 10001 kb in size you will not get
an error. In fact your code will not be executed at all. Instead the user will
get an DNS error saying “Cannot find server”. The Application refuses anything
larger than the maxRequestLength setting in the config file and hence your code never
gets executed.
The way to get past is to use your Application_BeginRequest
event to handle the problem.. This event takes place for each request to your
application BEFORE the data has been completely uploaded. . Here you
can check for the content length of the request and then redirect to the some
error page or the same page with some value in session or query string so that
the page can show appropriate message to the user.
protected void Application_BeginRequest(object sender, EventArgs e) { //This value is in bytes. int iMaxFileSize = 2097152; if (Request.ContentLength
> iMaxFileSize) { Response.Redirect("~/Pages/Error_Upload.aspx?size1="
+ Request.ContentLength); } }
You can change the value of iMaxFileValue to suitable value.
Here I am redirecting the page to an error page with the size of the file that the
user was trying to upload. In that page I give user an appropriate message. Hope This Helps Vikram
|
Posted on 10/10/2006 10:41:13 AM
Does Your redirection works well?
When redirecting, next request contains all data which is posted in first request. Therefore, Your example will become infinite redirection to specified page.
|
Posted on 3/15/2007 5:05:03 PM
Ive done this but im still getting the error. is this supposed to be webconfig in the wwwroot folder or the windows> .net>config file?
|
Posted on 5/21/2007 2:32:15 AM
Thanks for you post it really helped.
I just added the tag and problem was solved I was trying to upload a big size filr through FLEX on to Dotnet server and I was stuck and then I found this stuff it really clicked and things are working.
|
Posted on 5/24/2007 2:49:17 PM
Its not working!
As Drobit said, when redirecting, next request contains all data which is posted in first request.
So its showing the same DNS error.
|
Posted on 7/23/2007 6:40:22 PM
Is this code being put in the Global.asax file?
|
Posted on 7/23/2007 9:14:13 PM
Hi Leroy
Yes this code is put in global.asax
|
Posted on 10/11/2007 12:42:39 AM
Many thanks for post. This solved my problem.
|
Posted on 10/29/2007 6:20:25 AM
Hey guys.
I've made a little modification to the code depicted above.
Now, the redirect method is working well here.
protected void Application_BeginRequest(object sender, EventArgs e)
{
//This value is in bytes.
int maxFileSize = 2097152;
if (Request.ContentLength > maxFileSize)
{
int requestContentLength = Request.ContentLength;
if (Request.Files.Count > 0)
{
Request.Files[0].InputStream.Dispose();
}
Response.Redirect("~/Pages/Error_Upload.aspx?size1=" + requestContentLength);
}
}
Hope it helps. Cheers!
|
Posted on 11/5/2007 1:56:31 AM
I tested your script and it yet doesn't work. Once your file size exceed the limit stated in your web/machine config, no response.redirect will be executed.
|
Posted on 11/14/2007 10:00:06 AM
This will not work if the request size exceeds 10000
|
Posted on 11/30/2007 11:14:30 PM
I can't get it to work either, trying both this and trying to do a redirect out of the Error event. Are you setting your size limit in web.config larger than what you're checking for in code? That's the only way I can see this working.
|
Posted on 12/2/2007 4:37:55 AM
Hi Rayn,
You are right,I am setting the value larger in web.config.
|
Posted on 3/4/2008 3:49:45 PM
Thanks.
It works fine for me.
|
Posted on 4/4/2008 5:55:42 AM
hi!
I have a doubt.
It's works fine for me, however, it only executed, after the upload finished, if client select a file with 30Mb, it will take some time until redirect to error page. Right? or i'm wrong?
I think it's the life cycle of page.
Thanks
Kilop
teclahost.com
|
Posted on 4/7/2008 8:23:15 PM
Hi Kilop,
You are right. The page will be redirected only after the full upload.
Vikram
|
Posted on 4/9/2008 12:41:53 AM
Not working at all, I have tested using the both code. Still the error occur, it cannot able to redirect to defined aspx page.
|
Posted on 4/30/2008 3:42:09 AM
Thanks for your post, my prob solved.
|
Posted on 8/6/2008 10:37:17 PM
I tested your script and it yet doesn't work. Once your file size exceed the limit stated in your web/machine config, no response.redirect will be executed
|
Posted on 9/16/2008 6:54:32 PM
This is rubbish mate! Doesn't even work.
|