Last post Apr 14, 2009 08:41 PM by cool2000
Apr 14, 2009 05:45 PM|cool2000|LINK
Like some (or many), I want to be able to upload large files to the server with progress feedback WITHOUT purchasing an expensive control. I've read about HTTPModules and HTTPHandlers. Here's my code so far:
1 public class UploadHTTPModule : IHttpModule
3 // the initiation and other functions are not shown.
5 public void Application_BeginRequest(object source, EventArgs e)
7 HttpContext context = ((HttpApplication)source).Context;
11 if (context.Request.RawUrl.IndexOf("Upload") > 0)
if (context.Request.Files.Count > 0)
HttpPostedFile File = context.Request.Files;
// for debugging purposes.
context.Response.Write("FileName: " + System.IO.Path.GetFileName(File.FileName) + "; Size: "
// Here's where we Save them one chunk at a time.
UploadInfo U = new UploadInfo();
U.ContentLength = File.ContentLength;
U.FileName = U.FileName;
U.IsReady = true;
int increment = 1024;
int position = 0;
byte chunk = new byte[increment];
using (FileStream fs = new FileStream(context.Server.MapPath("/images/Content/") + System.IO.Path.GetFileName(File.FileName),
while (position < File.ContentLength)
// step 1: read the stuff to the buffer;
int chunkLength = (increment < File.ContentLength - position) ? increment : File.ContentLength - position;
int bytes = File.InputStream.Read(chunk, 0, chunkLength);
// step 2: write
fs.Write(chunk, 0, chunk.Length);
position += increment; // set up to read the next interval
U.UploadedLength = position;
context.Items["UploadInfo"] = U;
} // end while
} // end using
} // end if http files to be uploaded > 0
} // end if it's an upload page
} // end function
Yes, the file does get uploaded to the server where I tell it to. The picture looks right. Yes, the UploadHTTPModule does get executed because on post back, I see the debug information (lines 19 & 20). Now, I've got a button (whose onClick
event handler does nothing) and a FileUpload control on the page. That's the problem - it does a full postback when I click that button. As a result, the larger the file I want to upload, the longer the wait time - no different than saying MyFileUpload.SaveAs(...)
in the Button onClick event handler. It would then be pointless to make a progress indicator because it would only be updated once at 100% when the whole upload process is done.
> How do I make this asynchronous?
Now, I've got the UploadInfo class to keep track of where we're at in the chunk-at-a-time uploading. context.Session doesn't work, so I can't save it there. I noticed that context has an Items array, allegedly accessible from outside
the httpmodule code.
> How do I poll the server for progress? I can't find any code for this one - it's always just the upload by chunks that everybody has.
> ALSO, I did NOT use the HTTPWorkerRequest object. Is that absolutely necessary?
Apr 14, 2009 07:16 PM|whighfield|LINK
How about a Flash uploader?
Apr 14, 2009 08:41 PM|cool2000|LINK
Alright - I forgot to mention - Absolutely NO Flash, NO ActiveXObject (ADODB.Stream or FileSystemObject).
The swfupload - I'm so lost with implementing it - there seems to be too much work involved in getting it to work, and the documentation for it doesn't seem enough for me. Plus, I heard that it doesn't work with forms authentication when users are logged
in - it will load my login.aspx page on some div or iframe on the page it's in, and it wouldn't end up uploading the file. If what I just said about SWFUpload is not true, then, can somebody point me to a link that really shows us step-by-step on how to integrate
this in ASP.net?