Large file uploads in ASP.NET - Jon Galloway

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 8, 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 8, 2008 5:36 AM by Uwe

# re: Large file uploads in ASP.NET

Excellent post Jon, thanks.

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

# re: Large file uploads in ASP.NET

Information posted in a good way..

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

# 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 8, 2008 4:04 PM by Rick Strahl

# 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 9, 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 9, 2008 6:46 AM by suyog kale

# 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

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

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

# 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

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

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 8, 2008 1:46 AM by Anand

# 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

extreme process fuel summary stricter allows vapor vapor

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

# re: Large file uploads in ASP.NET

so, what's solution then?

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

# 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

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

# re: Large file uploads in ASP.NET

Monday, December 7, 2009 3:34 PM by CuteSoft_Components

# re: Large file uploads in ASP.NET

I want to ask how about that i want to upload a file such as HD Video which one is more than 4GB

Just use IIS7 and defualt ASP.NET 2.0 compent is ok?

Sunday, March 28, 2010 12:52 AM by zrxpower

# re: Large file uploads in ASP.NET

wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

Tuesday, April 20, 2010 3:59 AM by h

# re: Large file uploads in ASP.NET

any comments about DOWNLOAD file ?? thanks

Thursday, July 29, 2010 9:29 AM by alhambraeidos

# re: Large file uploads in ASP.NET

IIS7 could be config through web.config as well.

  <system.webServer>

    <security>

      <requestFiltering >

        <requestLimits maxAllowedContentLength="1073741824" ></requestLimits>

      </requestFiltering>

    </security>

  </system.webServer>

Tuesday, August 17, 2010 4:24 AM by bamboo82

# re: Large file uploads in ASP.NET

sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Tuesday, September 21, 2010 7:54 AM by sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

# re: Large file uploads in ASP.NET

Very Good Post. It really really Helpful.

Thursday, October 7, 2010 9:00 AM by Manoj Patil

# re: Large file uploads in ASP.NET

Thankzzzzzzzzzzzzzzzzzzzzzzzz,

Great Article...

Thursday, October 21, 2010 3:05 AM by Dennish

# re: Large file uploads in ASP.NET

Yeah its really well i thing this is not easy but i got best idea from this thanks

Tuesday, January 11, 2011 4:19 AM by Palanikumar

# re: Large file uploads in ASP.NET

This is very good article which i am looking for

Tuesday, January 11, 2011 12:25 PM by flash games

# re: Large file uploads in ASP.NET

FileUltimate is an ASP.NET file upload control which you can add directly to your existing ASP.NET (.aspx) pages.The control renders a user interface similar to "Windows Explorer" within the page which displays the contents of the target folder and accepts multiple file uploads from users. Actions can be limited by permissions and quota limits on folders. During file uploading, detailed information such as transfer speed and estimated time of completion are displayed along with the progress bar. This ASP.NET upload control supports browser upload,

ajax upload and flash upload modes.

Gleamtech.

fileupload asp.net

Sunday, January 30, 2011 3:51 PM by simoness

# re: Large file uploads in ASP.NET

fileupload asp.net

FileUltimate is an ASP.NET file upload control which you can add directly to your existing ASP.NET (.aspx) pages.The control renders a user interface similar to "Windows Explorer" within the page which displays the contents of the target folder and accepts multiple file uploads from users. Actions can be limited by permissions and quota limits on folders. During file uploading, detailed information such as transfer speed and estimated time of completion are displayed along with the progress bar. This ASP.NET upload control supports browser upload,

ajax upload and flash upload modes.

Sunday, January 30, 2011 3:53 PM by simoness

# re: Large file uploads in ASP.NET

great post, help me solve the problem !

Monday, March 14, 2011 3:54 PM by uwspstar

# re: Large file uploads in ASP.NET

I need to upload large files through FTP? Any ideas on why it doesn't work?

Wednesday, January 25, 2012 2:19 AM by Saeed Neamati

# re: Large file uploads in ASP.NET

Bags are believed being a good range of products for girls. Several women are enthusiastic about totes, plus they enjoy with the whole assortment of the new and trendy totes. When it comes to custom totes, oahu is the envision each woman to experience a large collection of primary artist purses, nevertheless the budget is indeed substantial who's will become challenging to afford all of them. In many cases, they can always choose custom reproduction artist bags.

Sunday, June 17, 2012 9:12 PM by Olganfh

# re: Large file uploads in ASP.NET

ASFDSDGSADSDFH  GJTRADFGASDGDFHAD

FGBNFSDGSADSDGASD SDGSDADFGASDGADFHAD

FGBNFSDGSADADSFHGADFS ZVXZADFGASDGSDFH

GJTRZSDGASDADFHGAD FGBNFASDGASDADSFHGADFS

Friday, August 31, 2012 1:21 AM by weareeThatt

# re: Large file uploads in ASP.NET

ASFDSDGSADSDFH  GJTRADFGASDGDFHAD

FGBNFSDGSADSDGASD SDGSDADFGASDGADFHAD

FGBNFSDGSADADSFHGADFS ZVXZADFGASDGSDFH

GJTRZSDGASDADFHGAD FGBNFASDGASDADSFHGADFS

Friday, August 31, 2012 1:21 AM by weareeThatt

Tuesday, September 4, 2012 10:45 AM by asdasd

# re: Large file uploads in ASP.NET

GJTRSDGSADDFHAD  QWERADFGASDGADSFHGADFS

SDGSDSDGSADDSFGHADS YUKYZSDGASDSDFH

SDGSDSDGSADDSFGHADS ERYERSDGSADDSFGHADS

ERYERADFGASDGDFHAD ZVXZADFGASDGASDGHASD

Wednesday, September 5, 2012 3:46 PM by GafeWrofe

# re: Large file uploads in ASP.NET

DSGASDGSADSDGASD  GJTRSDGSADADFHGAD

SDGSDSDGSADASDFHGAD  ADFHGSDGSADGDSFGHADS

DSGASDGSADGSDGASD  FGBNFSDGSADADFHAD

ZVXZADFHGDAFSDFH  YUKYSDGSADADFHAD

Thursday, September 6, 2012 1:34 AM by Absordreibe

# re: Large file uploads in ASP.NET

In you article, you claim that uploading large files is a missing browser feature.  It is not, and adding browser plugins (flash, Silverlight...) to the controls are thus not the solution.

The problem is that many web server page editing systems (such as ASP.NET, but also its competitors) make the mistake of first receiving the file somewhere temporary, then running the code that checks if the upload is allowed, thus creating both file size limits, DOS attack risks and slowness.

Old fashioned CGI scripts (remember those) will start running the server side code before receiving the file data and will let the code script handle the upload on the fly, thus solving all those problems.  Now if only IIS7 request scanning could be set to exclude the upload URL (which would then need a better code review to avoid holes).

Friday, September 21, 2012 9:52 AM by JB

# re: Large file uploads in ASP.NET

For below line

"FileSizeLimit.Text = string.Format("Make sure your file is under {0:0.#} MB.", maxFileSize);"

its saying

"The name 'FileSizeLimit' does not exist in the current context"

I am using .net MVC4

Tuesday, May 7, 2013 4:58 AM by Anshul

# re: Large file uploads in ASP.NET

What's up everyone, it's my first pay a quick visit at this website, and piece of writing is

actually fruitful for me, keep up posting these content.

Monday, May 13, 2013 5:21 PM by Thurston