FileUpload problems with ASP.Net and IIS7
By Mike Gledhill
ASP.Net features an easy-to-use FileUpload control.
It's very easy to write a simple FileUpload webpage, like the one below.
Here, the user would choose a file, click on an Upload button, and the app would copy the file to your web server.
However, by default, if you select a file over 4Mb and try to get your code to upload it to your server, the application
will crash as soon as it hits the SaveAs() command:
FileUpload1.PostedFile.SaveAs(TargetFilename);
Even adding an Exception handler around this line of code doesn't help.
Attempting to upload large files would still crash the app in an ugly way, and you can't intercept this problem.
However, you can increase the maximum allowed filesize by adding/changing a setting in the web.config file.
Here, we set the maximum allowed filesize to 50,000 Kb (about 50Mb).
<system.web>
<httpRuntime maxRequestLength="50000"/>
</system.web>
In IIS6, this is all you would need to do. Now, you would be able to upload files that are upto 50Mb in size. But with IIS7, things have changed.
With IIS7, you also need to add a second entry in your web.config file to say the same thing.
Notice how, in the code below, we specify (again) that we want a 50Mb maximum filesize, but this time, it has to be specified in bytes, rather than kilobytes.
<system.web>
<httpRuntime maxRequestLength="50000"/> <!-- Allow files of upto 50,000 Kb to be uploaded -->
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="50000000" /> <!-- Allow files of upto 50,000,000 bytes (about 50Mb) to be uploaded -->
</requestFiltering>
</security>
</system.webServer>
So, my rule of thumb: When you're using the ASP.Net FileUpload control, it's safest to always add both of these settings to your web.config file, to
cope with either versions of IIS. It'll save you hours of confusion and frustration later !
Comments