Sunday, January 01, 2006

ASP.Net FileUpload Size Problem

Hi,
Few months back, I was in my office enjoying my green tea and reading an article about Brad Pitt ad Angelina Jolie, suddenly my mIRC window started blinking, I opened it and to my horror, it was a personal message from one of the QA guys (who was testing my application), he was asking me that ‘is there any size limit on attachments?’ As I hadn’t placed any size constraint, I replied in negative, but I started to have a feeling that a bug is just around the corner and so it happened. He went excited and informed me with great enthusiasm that when he tries to upload a 5 MB file, application shows him a ‘Page Not Found’ error. I asked him to post the bug, then closed the article on the two Hollywood stars and in order to find the reason/solution I turned to my old buddy , ‘Google.’
Within 15 minutes I realized that every second ASP.Net programmer has faced this problem and still there is no good solution to it.
The problem was that by default ASP.Net ‘FileUpload’ control allows files of size 4 MB or less, anything bigger then that size will redirect you to a DNS error. The main rason behind this constraint is to prevent denial of service attack incase users posts files of extremely large sizes.
The solution was quite simple, in web.config’s element; you just need to set the value of ‘maxRequestLength’ to your desirable maximum file size. easy!
But, it’s not that easy, anything greater then that size will still redirect you to DNS error page. We certainly don’t want to let our users see that page, instead we would like to show them a good error message saying something like ‘files size should be less then {0}.’ For your disappointment, I must confess that there is simply no solution to it. You just don’t have any control on the process; no implementation of IHttpHandler can help you in this regard. One of our friend’s on this link tried to provide a solution but it just doesn’t work for about 90% of the people (including me). Whenever the file size was too large, the Reques.Files.Count was somehow ‘0.’ All I can say is that a solution, which does not work for everyone, is just not a solution.
Microsoft provides a little bit of help, which you may access at,
http://support.microsoft.com/default.aspx?scid=kb;en-us;295626

Now, that’s not all, there is something more to this problem, ASP.Net uses physical memory to upload a file and whenever ASP.Net process uses 60% of RAM, .Net services restarts. Tests have proved that a 200 MB file can crash a system with 512 MB RAM. Hopefully, you can appreciate the magnitude of the problem. And to make it a bit more convoluted, IIS has its own file size limit of 4 GB.

If you’ve visited the MS link, then you can see that MS knows about the DNS thing, still they are trying to become oblivious to it, so we should read between the lines, that they want us to go for a third part control. Yes, if your expected file size is small then go for this control, otherwise I would recommend you to go for a 3rd party control. These days I am writing one for myself and will soon upload it.

That’s all I have to say on this topic, for the next post I have quite a few topics in my mind like Custom Membership provider/ Inheriting GenericIdentity , but not sure when I will be able to write them, a lot of things to do including my ASP.Net’s MCP paper, IELTS test and a project at office. I will try to come back soon.

Happy New Year to you all,
Beenz

Labels: ,