Scenario :
I had a requirement to develop a page which has to upload the file to the DB server. Uploading a file to the server is a very easy thing with the FileUpload control. By default the maximum size allowed to upload is 4MB and if you want you can increase or decrease in web.config settings.
Problem :
There won't be any problem, if you upload the file size of less than the specified size in web.config. If the size of the file is more than the limit specified, the runtime will be thrown with the message of "Maximum Request Length Exceeded". The thing is it won't be catched in button click of the File upload. Let us see how can we fix this. I found the solution in web and explaining for you.
Solution:
I'm not sure this is the best way to do it. But this also a solution. We could catch this in Application_BeginRequest event of Global.asax. Normally whatever the client request for a page or file this application BeginRequest event will be fired and the control goes to the page events. Since its going for all the postback and initial load. We can calculate the content length of each post by the below lines,
HttpContext context = ((HttpApplication)sender).Context;
int PageContentLength = context.Request.ContentLength;
In addition with that, we need to calculate the actual size of the page and do some calculations. Look at the entire code below, which have to be added in Global.asax.
The last line redirects the control to the same page with the url querystring. Based on the query string, we'll display the error message in Attachment page. Another thing here is as soon as you displayed the error message you need to remove the querystring value.
Conclusion:
This solution may not be better for performance wise since this event is keep calling for all the page requests as well file requests like .JS or .CSS. Let me know if you have better solution than this.
Happy Coding....
I had a requirement to develop a page which has to upload the file to the DB server. Uploading a file to the server is a very easy thing with the FileUpload control. By default the maximum size allowed to upload is 4MB and if you want you can increase or decrease in web.config settings.
Problem :
There won't be any problem, if you upload the file size of less than the specified size in web.config. If the size of the file is more than the limit specified, the runtime will be thrown with the message of "Maximum Request Length Exceeded". The thing is it won't be catched in button click of the File upload. Let us see how can we fix this. I found the solution in web and explaining for you.
Solution:
I'm not sure this is the best way to do it. But this also a solution. We could catch this in Application_BeginRequest event of Global.asax. Normally whatever the client request for a page or file this application BeginRequest event will be fired and the control goes to the page events. Since its going for all the postback and initial load. We can calculate the content length of each post by the below lines,
HttpContext context = ((HttpApplication)sender).Context;
int PageContentLength = context.Request.ContentLength;
In addition with that, we need to calculate the actual size of the page and do some calculations. Look at the entire code below, which have to be added in Global.asax.
Event in Global.asax |
The last line redirects the control to the same page with the url querystring. Based on the query string, we'll display the error message in Attachment page. Another thing here is as soon as you displayed the error message you need to remove the querystring value.
Conclusion:
This solution may not be better for performance wise since this event is keep calling for all the page requests as well file requests like .JS or .CSS. Let me know if you have better solution than this.
Happy Coding....