Advanced download of Asp.Net files
When using the standard <input type="file" /> on the mvc3 website, you can get the file in your action method by creating an input parameter of type HttpPostedFile and setting the form to enctype="multipart/form-data"
One of the problems with this approach is that the request does not end and is not passed to the action method until the entire contents of the file are loaded.
I would like to do some things for this file as it is uploaded to the server. Basically, I want to receive data asynchronously as they arrive, and then programmatically process data bytes by byte.
To accomplish the above, I assume that you will need to process this part of the request in an HttpModule or a custom HttpHandler. I am familiar with how this works, but I am not familiar with the method of retrieving file upload data asynchronously when it arrives.
I know that this is possible because I have worked with third-party components in the past that do this (as a rule, they can report download progress or cache data on disk to avoid iis / asp.net memory limitations). Unfortunately, all the components that I used are private, so I canβt look inside and see what they do.
I'm not looking for code, but can someone lead me in the right direction here?
Using the WCF service, you can send file streams to and from your service.
The following is the service access code that I am using:
int chunkSize = 2048; byte[] buffer = new byte[chunkSize]; using (System.IO.FileStream writeStream = new System.IO.FileStream(file.FullName, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)) { do { // read bytes from input stream int bytesRead = request.FileByteStream.Read(buffer, 0, chunkSize); if (bytesRead == 0) break; // write bytes to output stream writeStream.Write(buffer, 0, bytesRead); } while (true); writeStream.Close(); } If this looks like what you want, check out CodeProject File Transfer . It has a lot of details about what my code is freely based on.