Large file uploads in ASP.NET

Uploading files via the FileUpload control gets tricky with big files. The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded."

Increasing the Maximum Upload Size

The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, you'd do this:

<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>

Since the maximum request size limit is there to protect your site, it's best to expand the file-size limit for specific directories rather than your entire application. That's possible since the web.config allows for cascading overrides. You can add a web.config file to your folder which just contains the above, or you can use the <location> tag in your main web.config to achieve the same effect:

<location path="Upload">
    <system.web>
        <httpRuntime executionTimeout="110" maxRequestLength="20000" />
    </system.web>
</location>

What Happens When I Upload A File That's Too Big?

While expanding the upload restriction is a start, it's not a full solution for large file uploads. Milan explains one of the biggest problems with large file uploads in The Dark Side Of File Uploads:

It gets really interesting if someone uploads a file that is too large. Regardless of what your maxRequestLength setting mandates, IIS has to guzzle it, and then ASP.NET checks its size against your size limit. At this point it throws an exception.

As Milan explains, you can trap the exception, but it's trickier than you'd expect. He talks about overriding Page.OnError and checking for HTTP error code 400 when the error is HttpException, which as he says is less than ideal.

At Least Give Me A Warning

If we've got a set limit on file upload sizes, we should at least tell our users what it is. Since this is a configurable value which we may change later, the best is to make our file size warning read directly from web.config setting. The best way to do this is to pull back the httpRuntime section as a HttpRuntimeSection object, which isn't too hard given:
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
double maxFileSize = Math.Round(section.MaxRequestLength / 1024.0, 1);
FileSizeLimit.Text = string.Format("Make sure your file is under {0:0.#} MB.", maxFileSize);

A Real Solution: an HttpModule to Handle File Uploads

There are better solutions to handling large file uploads in ASP.NET. A custom HttpHandler can provide a better user experience by displaying upload progress and allowing you to handle a file size problem in a more controlled fashion. Here's a summary from a cursory search:

One of the longest running threads on the ASP.NET Forums (going back 5 years): HttpHandler or HttpModule for file upload, large files, progress indicator?

A Better Solution: a RIA upload component

The project which caused me to look into this, Video.Show, was based on ASP.NET and Silverlight 1.0, so we weren't able to take advantage of RIA platforms which would support more advance upload handling. In most cases, though, I'd recommend replacing the FileUpload component with a Silverlight or Flash based file upload control. In addition to a better upload experience, these controls generally look better than the the generic button displayed for the <input type="file"> element which is rendered by the FileUpload control. The input / file element doesn't allow for CSS formatting, although smart CSS hackers always seem to find a way around these things.

Although there doesn't seem to be a project / component to support Silverlight uploads yet, Liquid Boy has a nice sample of a Silverlight 1.1 (oops, 2.0) based upload control. I've heard good things about SWFUpload, a Flash and JavaScript based upload system. Developers are responsible for handling a few JavaScript events as well as accepting the file on the server. That can be done pretty easily, as you can see from this ASP.NET sample implementation. Here's a screenshot from one of the SWFUpload online demos:

SWFUpload

What About IIS7?

Oh, and there's more thing to worry about. IIS7 has a built-in request scanning which imposes an upload file cap which defaults to 30MB. Again, this is a good feature, but it gets in the way if you're looking to upload files larger than 30MB. Steve Schofield posted about how to change this from the comandline:

appcmd set config "My Site/MyApp" -section:requestFiltering -requestLimits.maxAllowedContentLength:104857600 -commitpath:apphost

Why is this such a pain?

Browsers, and HTML in general, were never designed to handle large uploads gracefully. Jeff Atwood and I discussed this back in September, and he summed up the issue pretty well in his post asking Why Are Web Uploads So Painful? It's disappointing that browser standards have failed us to the point that we need to use browser extension technologies like Flash and Silverlight as a band-aid here. Browsers should support uploads over both HTTP and FTP (it is the file transfer protocol, after all), and should manage the upload in a side or toolbar that allows us to continue browsing without breaking the upload. The Firefox Universal Upload add-on is an example of how this should work out of the box.
 
Historical trivia: In ASP.NET 1.0 and 1.1, the entire file was loaded into memory before being written to disk. There were improvements in ASP.NET 2.0 to stream the file to disk during the upload process.
Published Tuesday, January 08, 2008 1:13 AM by Jon Galloway

Comments

# re: Large file uploads in ASP.NET

In my "hobby project" at www.zeta-uploader.com, I developed the Windows client to split larger chunks into smaller pieces (about 50k IIRC) to be more error tolerant to network issues.

Tuesday, January 08, 2008 5:36 AM by Uwe

# Large file uploads in ASP.NET &raquo; article &raquo; Thats The New Thing!

Pingback from  Large file uploads in ASP.NET &raquo; article &raquo; Thats The New Thing!

# re: Large file uploads in ASP.NET

Excellent post Jon, thanks.

Tuesday, January 08, 2008 8:23 AM by Mark Wisecarver

# re: Large file uploads in ASP.NET

We use SFWUpload for Graffiti and it works really well except for a minor issue with FireFox.

Scott

Tuesday, January 08, 2008 9:05 AM by Scott Watermasysk

# re: Large file uploads in ASP.NET

Information posted in a good way..

Tuesday, January 08, 2008 9:31 AM by Haissam Abdul Malak

# 4 Links Today (2008-01-08)

Pingback from  4 Links Today (2008-01-08)

Tuesday, January 08, 2008 10:19 AM by 4 Links Today (2008-01-08)

# Large file uploads in ASP.NET

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Tuesday, January 08, 2008 4:02 PM by DotNetKicks.com

# re: Large file uploads in ASP.NET

Jon, wouldn't you say that part of the reason Uploads from the browser are difficult is that if it were easy we'd have major abuse issues? By requiring a separate control and JavaScript automation of those controls at least you limit the effect of bots that could automate the process.

I agree though it'd be extremely nice if there was some way to handle uploads in a more consistent way bypassing the whole multi-part mess. Ideally being able to stream the raw data would be ideal (since that's what you can do with Flash and SL 2.0 anyway). But even the abillity to select more than one file at a time in the file upload dialog I suppose would be useful enough. <shrug>

Tuesday, January 08, 2008 4:04 PM by Rick Strahl

# links for 2008-01-09 | Funny Stuff is all around

Pingback from  links for 2008-01-09 | Funny Stuff is all around

Tuesday, January 08, 2008 7:32 PM by links for 2008-01-09 | Funny Stuff is all around

# re: Large file uploads in ASP.NET

You can also use a Web Service with Authentication to send chunks of a file over HTTP and then rebuild on the other side.

I agree that this needs to be fixed somehow. Hopefully the next version of ASP.NET will have something built in.

Wednesday, January 09, 2008 2:51 AM by John Sedlak

# re: Large file uploads in ASP.NET

is there any web service , so we can refer & use in our web application to upload large file

Wednesday, January 09, 2008 6:46 AM by suyog kale

# re: Large file uploads in ASP.NET

"Although there doesn't seem to be a project / component to support Silverlight uploads yet"

Actually there is such control - Telerik have a silverlight1.1 based upload:

www.telerik.com/.../index.html

Wednesday, January 09, 2008 7:51 AM by Jody

# re: Large file uploads in ASP.NET

Suyog Kale - I have created an article on how I did it using C# here: jsedlak.com/Content.aspx

I am unsure how it would work in a web page scenario, you would most likely need an ActiveX control to chunk the local file.

Hope you find it useful!

Wednesday, January 09, 2008 12:54 PM by John Sedlak

# Three posts a week - my new year's resolution

I'll pass my 5 year blogging anniversary this year. I've written over 520 posts. But, I'd like to be

Saturday, January 12, 2008 4:31 AM by Jon Galloway

# W&ouml;chentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, Data Services, C# 3.0, WPF, System.AddIn, AJAX&#8230; | Code-Inside Blog

Pingback from  W&ouml;chentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, Data Services, C# 3.0, WPF, System.AddIn, AJAX&#8230; | Code-Inside Blog

# re: Large file uploads in ASP.NET

We've been using SWFUpload for a while now. Works well for about 80% percent of our users, but has unresolved issues for the rest. I really like the concept and the architecture of it, but it just seems that with Flash there are alot of open issues. Specially with SSL and Proxy's, which is very unfortunate...

Monday, January 21, 2008 9:10 AM by Remy

# re: Large file uploads in ASP.NET

Very useful and timely for me - thanks for taking the time!

Friday, January 25, 2008 7:45 AM by Gregor Spowart

# re: Large file uploads in ASP.NET

I wrote SlickUpload (krystalware.com/.../SlickUpload), one of the original upload components from 4 or 5 years ago -- another one for your list.

It's still going strong today. I'm definitely thinking of adding a RIA component using flash and/or silverlight. However, the capability to work with no browser plugins is key for many clients.

Friday, January 25, 2008 9:46 AM by Chris Hynes

# Jan 24th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET, IIS

I just arrived back from my trip from Asia, and decided to celebrate (since I&#39;m jet-lagged and can&#39;t

Friday, January 25, 2008 10:27 AM by Blogs

# Enlaces de Enero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET, IIS &laquo; Thinking in .NET

Pingback from  Enlaces de Enero: ASP.NET, ASP.NET AJAX, Visual Studio, .NET, IIS &laquo; Thinking in .NET

# 大文件上传相关资源

1、Large file uploads in ASP.NET:weblogs.asp.net/.../large-file-uploads-in-asp-net.asp...

Monday, January 28, 2008 9:27 AM by 梦想永存的博客

# Chief of the System Blog &raquo; File Upload with ASP.NET

Pingback from  Chief of the System Blog &raquo; File Upload with ASP.NET

Wednesday, January 30, 2008 4:30 AM by Chief of the System Blog » File Upload with ASP.NET

# re: Large file uploads in ASP.NET

I was looking at the same issue. Would love to hear what components other people are using?

The results from my little evaluation are here:

remy.supertext.ch/.../file-upload-with-aspnet

Wednesday, January 30, 2008 4:32 AM by Remy

# Code Pagoda &raquo; Blog Archive &raquo; Large File Uploads in ASP.NET

Pingback from  Code Pagoda  &raquo; Blog Archive   &raquo; Large File Uploads in ASP.NET

Wednesday, January 30, 2008 4:13 PM by Code Pagoda » Blog Archive » Large File Uploads in ASP.NET

# Upload di file di grosse dimensioni

Upload di file di grosse dimensioni

Thursday, January 31, 2008 10:03 AM by Marco Bellinaso

# re: Large file uploads in ASP.NET

I wrote an upload control using Flex and ASP.Net.  It is at www.codeproject.com/.../FlashUpload.aspx.  It has a few issues but I believe it is mostly Flash related.  I use it on three different websites and haven't had any trouble (that I know of).  I'm hoping with Silverlight 2.0, a better one can be made.  I provide the source code for the flex and ASP.Net.

Wednesday, February 06, 2008 3:45 PM by Darick

# re: Large file uploads in ASP.NET

SAFileUp is one of the most mature web upload components around.  It dates back to the 90s - how'd you miss it?

fileup.softartisans.com

Wednesday, February 13, 2008 12:26 AM by Steve Smith

# File Upload with ASP.NET

Would really love to hear what experiences that other people had?

Tuesday, March 04, 2008 4:11 AM by coofucoo zhang

# re: Large file uploads in ASP.NET

FileUp from Softartisans does not work well with Safari, which is a killer argument for most public websites.

Wednesday, March 05, 2008 6:14 AM by Remy

# re: Large file uploads in ASP.NET

dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

Thursday, April 03, 2008 8:41 AM by rrrr

# re: Large file uploads in ASP.NET

dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

Thursday, April 03, 2008 8:44 AM by rrrr

# re: Large file uploads in ASP.NET

We use <a href="ajaxuploader.com/">Ajax Uploader</a>. It works great!

Wednesday, April 09, 2008 4:15 PM by Jason

# re: Large file uploads in ASP.NET

Hello there,

I want to upload file siezes upto 50 MB( Video and Audio) to my website.

I successfully able to upload video size of 20 MB. But more then cause errors.

I did the following changes but no success..

<system.web>

 <httpRuntime executionTimeout="2400" maxRequestLength="20480" />

</system.web>

and also change 1 entry in the following file

C:\WINDOWS\system32\inetsrv\MetaBase.xml

AspMaxRequestEntityAllowed

But No success can anyone help??

Thanks

Imran Khan

imran@bcinewmedia.com

mikhan921@hotmail.com

Friday, April 18, 2008 9:17 AM by mikhan921

# re: Large file uploads in ASP.NET

Has anyone tried the WSE??? like it? hate it?

Friday, May 16, 2008 3:54 PM by juztin

Leave a Comment

(required) 
(required) 
(optional)
(required)