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.

41 Comments

  • 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.

  • Excellent post Jon, thanks.

  • Information posted in a good way..

  • 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.

  • 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.

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

  • 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...

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

  • 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..




    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




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

  • EAUpload component http://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.

  • I'm trying to upload files on the server (running IIS) using 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
    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 ????

  • 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.

  • 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:http://www.telerik.com/help/aspnet-ajax/upload_uploadinglargefiles.html

    Hoped this helped

  • extreme process fuel summary stricter allows vapor vapor

  • so, what's solution then?

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

    thanks

  • THANKS!!!!

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

    Lovely

  • 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?

  • http://ajaxuploader.com/Demo/Large-File-Upload.aspx

  • 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?

  • wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

  • any comments about DOWNLOAD file ?? thanks

  • IIS7 could be config through web.config as well.








  • sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

  • Very Good Post. It really really Helpful.

  • Thankzzzzzzzzzzzzzzzzzzzzzzzz,
    Great Article...

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

  • This is very good article which i am looking for

  • 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

  • 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.

  • great post, help me solve the problem !

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

  • 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.

  • ASFDSDGSADSDFH GJTRADFGASDGDFHAD
    FGBNFSDGSADSDGASD SDGSDADFGASDGADFHAD
    FGBNFSDGSADADSFHGADFS ZVXZADFGASDGSDFH
    GJTRZSDGASDADFHGAD FGBNFASDGASDADSFHGADFS

  • ASFDSDGSADSDFH GJTRADFGASDGDFHAD

    FGBNFSDGSADSDGASD SDGSDADFGASDGADFHAD

    FGBNFSDGSADADSFHGADFS ZVXZADFGASDGSDFH

    GJTRZSDGASDADFHGAD FGBNFASDGASDADSFHGADFS

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

  • GJTRSDGSADDFHAD QWERADFGASDGADSFHGADFS
    SDGSDSDGSADDSFGHADS YUKYZSDGASDSDFH
    SDGSDSDGSADDSFGHADS ERYERSDGSADDSFGHADS
    ERYERADFGASDGDFHAD ZVXZADFGASDGASDGHASD

  • DSGASDGSADSDGASD GJTRSDGSADADFHGAD
    SDGSDSDGSADASDFHGAD ADFHGSDGSADGDSFGHADS
    DSGASDGSADGSDGASD FGBNFSDGSADADFHAD
    ZVXZADFHGDAFSDFH YUKYSDGSADADFHAD

  • 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).

  • 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



  • 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.

Comments have been disabled for this content.