Streaming Large File Uploads in ASP.NET Streaming Large File Uploads in ASP.NET asp.net asp.net

Streaming Large File Uploads in ASP.NET


You can handle uploads in a completely customized way without buffering usingHttpRequest.GetBufferlessInputStream method. Basically you are getting access to the raw incoming data and free to do whatever you want with it.

I've just created small sample which saves raw request content to a file:

  1. Create handler:

    public class UploadHandler : IHttpHandler{    public void ProcessRequest(HttpContext context)    {        using (var stream = context.Request.GetBufferlessInputStream())        using (var fileStream = File.Create("c:\\tempfile.txt"))        {            stream.CopyTo(fileStream);        }    }    public bool IsReusable { get {  return true; } }}
  2. Register in Web.config:

    <system.webServer>    <modules runAllManagedModulesForAllRequests="true"/>    <handlers>        <add name="UploadHandler" verb="POST"            path="/upload"            type="UploadHandler"        resourceType="Unspecified"/>    </handlers></system.webServer>
  3. Create a page with a form:

<form action="/upload" method="post" enctype='multipart/form-data'>     <input type="file" name="aa" id="aa"/>     <input type="submit"/></form>


If the uploading and streaming is using up valuable server resources then you might wanna take a look at hosting your media files on a cloud of some sort. It's possible with ASP.NET to use a Rackspace, Amazon Cloud API have your users upload the files directly to a CDN network and then serve the content that way, I know this isn't answering your question but many people will or already have and thought I'd get my 2 cents in. Many people still not opting to use the cloud amazes me! once you go CDN you never go back. Furthermore with most CDN's you will also be given a streaming URL for your upload container where it supports lots of different movie types, and its lighting fast, not only for your users to upload too but also your never have slow speeds on your website as a result.