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

# re: Large file uploads in ASP.NET

You can also use an applet on client side such as jfileupload: www.jfileupload.com/.../index.html

It has been tested with ASP.NET available at:

www.jfileupload.com/.../index.html

Tuesday, May 20, 2008 2:55 PM by JF

# Large File Uploads For Clients

Large File Uploads For Clients

Tuesday, June 10, 2008 12:32 PM by Gerry Heidenreich

# Moms work at home.

Work at home moms message boards.

Thursday, June 12, 2008 12:45 AM by Voyforums work at home moms.

# ASP.Net Large File Upload (im Classic Mode und die Begrenzung des HTTP Requests

ASP.Net Large File Upload (im Classic Mode und die Begrenzung des HTTP Requests

Friday, June 13, 2008 6:27 AM by Weltretter.com

# re: Large file uploads in ASP.NET

EAUpload component www.easyalgo.com/EAUpload.aspx.

It has many standart and advanced features such as larg files upload, setting customization for each upload (maximum request length, execution time out, bandwidth limitation, etc), custom handle exception of upload process.

One another asp.net upload component to your list.

Thursday, June 19, 2008 3:32 AM by GregoryPankov

# re: Large file uploads in ASP.NET

Please provide me a guidance that where to mention the server name or ip address.

I could not able to upload the files to the server. it is upoloading into the local machine only.

Please help me out

Thursday, June 19, 2008 6:25 AM by Alina

# re: Large file uploads in ASP.NET

I'm trying to upload files on the server (running IIS) using <input type=file name=myfile> in my ASP.net application (.net 2003).It works fine on local system but when I try this on the server it uploads only small files of size max 50Kb.Attempts to upload larger file end up with an error saying : Could not find a part of the path "C:\Inetpub\wwwroot\myweb\Academic\". source : mscorlib

at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at sdmsystem.Upload.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\myweb\Upload.aspx.vb:line 32 at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain()

I have already made the following changes in my web.config file

<httpRuntime executionTimeout="900" maxRequestLength="20000" /> and given full permissions on the folder to all users.

But could not find a solution.I'm using the follwing code:

If Request.Files.Count = 1 Then

   fName = myFile.PostedFile.FileName

   fName = Right(fName, (fName.Length - fName.LastIndexOf("\")) - 1)

   Dim FilePath As String

   FilePath = Server.MapPath("")& "\Academic\" & fName

   myFile.PostedFile.SaveAs(FilePath)

   lblMsg.Text = fName

End If

Am I missing something ????

Tuesday, July 08, 2008 1:46 AM by Anand

# ASP.NET File Upload with *Real-Time* Progress Bar - redux @ ZDima.net

Pingback from  ASP.NET File Upload with *Real-Time* Progress Bar - redux @ ZDima.net

# Ultracet medication.

Extracting acetaninophen from ultracet. Ultracet addiction. Ultracet.

Tuesday, August 05, 2008 2:45 AM by Ultracet.

# re: Large file uploads in ASP.NET

An control to upload files is included in the suite of controls provided by ComponentOne (Studio for Silverlight).

It also includes the handlers in the server side to rebuild the files if you need to split them in smaller parts.

Tuesday, September 30, 2008 10:09 PM by leovernazza

# re: Large file uploads in ASP.NET

for those who have tried everything to upload large files using ASP controls but it still doesn't work, don't forget to check the urlscan.ini file.  key = MaxAllowedContentLength

this url helped me:www.telerik.com/.../upload_uploadinglargefiles.html

Hoped this helped

Thursday, October 16, 2008 2:54 PM by yohannn

# re: Large file uploads in ASP.NET

You definitely miss Velodoc XP Edition which is open source and can be downloaded from www.codeplex.com/velodocxp.

With Velodoc XP Edition, you not only have ASP.NET upload and download components based on ASP.NET Ajax Extensions, you also get a managed file transfer application integrated with Microsoft Outlook.

For more information, please check http://www.velodoc.com

Thursday, November 27, 2008 9:41 AM by jlchereau

# re: Large file uploads in ASP.NET

Velodoc XP Edition is open source - however wrt the licencing :-

www.codeplex.com/.../license

www.velodoc.com/.../membership.htm

- is it actually free ?

Tuesday, January 20, 2009 7:41 AM by travisingram

# re: Large file uploads in ASP.NET

Thanks, great article. Good summary and gives me loads of things to try.

Friday, January 23, 2009 4:29 PM by Trevor

# re: Large file uploads in ASP.NET

Hello,

Take a look to another flash upload utility EAFlashUpload:

www.easyalgo.com/eaflashupload.aspx. It has three different user interfaces and many advanced features.

One another flash upload component to your list.

Tuesday, March 17, 2009 6:04 AM by Serg

# re: Large file uploads in ASP.NET

<a href= kingdavidgd.causeauts.info >king david gd</a>  

<a

Saturday, April 25, 2009 9:17 PM by Halo

# re: Large file uploads in ASP.NET

extreme process fuel summary stricter allows vapor vapor

Friday, May 08, 2009 5:40 AM by galactic apple countries home

# re: Large file uploads in ASP.NET

Saturday, May 09, 2009 9:25 AM by Heel

# re: Large file uploads in ASP.NET

<a href= http://newjersey

Tuesday, May 12, 2009 12:54 PM by Halo

# re: Large file uploads in ASP.NET

Friday, May 15, 2009 6:39 AM by Heel

# re: Large file uploads in ASP.NET

sFileDir

Thursday, June 04, 2009 2:56 AM by kumararvind87

# re: Large file uploads in ASP.NET

so, what's solution then?

Friday, June 05, 2009 6:34 PM by b2b

# re: Large file uploads in ASP.NET

<a href= belksdepartmentstoresinflorida.hweusqv.cn >belks department stores in florida</a>  

<a href= http:/

Thursday, June 11, 2009 11:51 PM by Heel

# ASP.NET 学习资料分享

Tuesday, June 23, 2009 3:06 AM by ^_^克方

# re: Large file uploads in ASP.NET

<a href= thekiterunnerchaptersummaries.awqyrs.cn >the kite runner chapter summaries</a>  

<a href= http:

Thursday, July 02, 2009 10:05 PM by Bill

# re: Large file uploads in ASP.NET

Thursday, August 13, 2009 4:33 PM by Heel

# re: Large file uploads in ASP.NET

and for download big big files in IIS for xp, and iis 6.0 for w2003 ??

thanks

Friday, August 14, 2009 11:14 AM by eeee

# re: Large file uploads in ASP.NET

<a href= bleachmugencharacterdownload.beforeoufs.info >bleach mugen chara

Sunday, August 23, 2009 11:46 AM by Halo

# re: Large file uploads in ASP.NET

<a href= demoulasmarketbasketchelseamasslocation.beforeoufs.info >demoulas market basket chelsea mass location</a>  

<a

Sunday, August 23, 2009 9:31 PM by Halo

# re: Large file uploads in ASP.NET

<a href= http://mo

Wednesday, August 26, 2009 8:36 PM by Arnie

# re: Large file uploads in ASP.NET

A facility named upload.com already exists for this. Mideafire is also there for larger uploads.

Monday, October 05, 2009 6:09 AM by nintendo ds r4

# re: Large file uploads in ASP.NET

Hello,

Take a look to another flash upload utility EAFlashUpload:

www.easyalgo.com/eaflashupload.aspx.

Monday, October 12, 2009 3:56 AM by r4 firmware

# Uploading large files with ASP.NET &laquo; Ad hoc Geek

Pingback from  Uploading large files with ASP.NET &laquo;  Ad hoc Geek

Tuesday, October 20, 2009 7:29 AM by Uploading large files with ASP.NET « Ad hoc Geek

# re: Large file uploads in ASP.NET

THANKS!!!!

You just saved my day.. Needed it a work - a long time problem solved.

Lovely

Friday, October 23, 2009 4:55 AM by Mcoroklo

# re: Large file uploads in ASP.NET

There's a lot of mention of different technologies - too many for anyone to decide on. What method/program do the big sites use? Hotmail.com, Yahoo.com, Gmail.com, Facebook.com, etc...

any thoughtS?

Tuesday, November 24, 2009 5:47 PM by Iandawg

Leave a Comment

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