This is not a new thing by any means, but I found it difficult to find a single place where it was explained and the code made available, so here we go ...

This can be done by an HttpModule which will interrupt the usual request flow and allow for progress to be monitored. The key is to get hold of the HttpWorkerRequest and read the data as it comes in, keeping a track of the number of bytes received against the total gives you the progress.

Posting the file(s) and checking the progress can be done using AJAX, I've used the jQuery Form plugin.

  1. Create a unique progressId, this is detected by the HttpModule and triggers the progress tracking
  2. Post the File(s), adding the progressId to the query string
  3. Store the request in a static collection, making it accessible to subsequent requests
  4. Start reading the incoming data to a temporary file - I did do this in memory at first, but allocating a byte array for the lifetime of a possibly long request seem a bit of a waste of scarce resource, so its saved to disk instead
  5. Make further requests using a specific URL and the progressId
  6. When the request is complete, load the file into a byte array and pass it on as preloaded content to the next module. This means the usual flow is resumed and you get the files loaded in the Request.Files collection as you would expect

You need to decide how big the files you are going to allow will be. You can increase the maxRequestLength to ~2GB in your web.config if you like, but fundamentally you will be limited to the physical memory available to store the request in a byte array when passed on as preloaded content in step 6 above, on a shared server this will be considerably less than 2GB.

<system.web>
  <otherstuff />
  <httpRuntime maxRequestLength="2097151"/>
</system.web>

You will need to register the module in your web.config httpModules

<system.web>
  <httpModules>
    <others />
    <add name="ProgressModule" type="Antix.Web.ProgressModule"/>
  </httpModules>
</system.web>

Download the MVC project below, with all the code mentioned

Requires .NET 3.5, ASP.NET MVC 1.0

File Upload with Progress Example Project

p.s. the module can be used in non-MVC projects, its just the example is an MVC project.

comments powered by Disqus

On Twitter Follow MrAntix on Twitter

Soft_dooM 47 minutes ago
Soft_dooM

Utilizing Home windows Azure Cloud for Learners http://t.co/T9f5VNn75o http://t.co/pQKy1kepG8

NeuvooOCCA 50 minutes ago
NeuvooOCCA

Looking for a #Lead #.NET Developer - C#, http://t.co/x2Q4WNfowk, MVC #jobs http://t.co/gWmN2KbcOe pic.twitter.com/lqdNcv0ZLZ

p2pWebMobileIt One hour ago
p2pWebMobileIt

.NET Developer, http://t.co/0VmADUn9yy MVC, C# - London w/ #dotNET #MVC skills United Kingdom @p2people https://t.co/zdeJuN2J8k

tutslycom 1 hours ago
tutslycom

[http://t.co/LL9mgLMrnn MVC TUTORIAL] LECTURE 29 NOTRACKING QUERIES - http://t.co/63KiblyVuu #tutorial pic.twitter.com/C7wbefejNN

tutslycom 1 hours ago
tutslycom

[http://t.co/LL9mgLMrnn MVC TUTORIAL] LECTURE 22 ENTITY SPLITTING - http://t.co/y2yp6JeoYB #tutorial pic.twitter.com/nGFQZEh6Rz