6

When using a standard <input type="file" /> on an mvc3 site, you can receive 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 of this approach is that the request does not complete and is not handed off to your action method until the entire contents of the file have been uploaded.

I would like to do some things to that file as it is being uploaded to the server. Basically i want to asynchronously receive the data as it comes in and then programmatically handle the data byte by byte.

To accomplish the above I imagine you will need to handle this part of the request in an HttpModule or custom HttpHandler perhaps. I am familiar with how those things work, but I am not familiar with the method of receiving the file upload data asynchronously as it comes in.

I know this is possible because I have worked with 3rd party components in the past that do this (normally so they can report upload progress, or cache the data to disk to avoid iis/asp.net memory limitations). Unfortunately all the components I have used are closed source so I can't peek inside and see what they are doing.

I am not looking for code, but can someone get me pointed in the right direction here?

2
  • Have you looked at passing the request off to a WCF Service? Commented Jan 10, 2012 at 23:28
  • Have a look at Jon Galloway's post, it may point you in the right direction (specifically his mention of NeatUpload looks promising). Commented Feb 11, 2012 at 21:11

1 Answer 1

1

Using a WCF service you can send file streams to and from your service.

Here is the service side receive code I use:

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 that looks like what you want, check out the CodeProject File Transfer Progress. It goes into a lot of detail that my code is loosely based on.

Sign up to request clarification or add additional context in comments.

3 Comments

Can this be used to upload a multipart form from a web browser?
While this is interesting and I appreciate the time taken to post this, It doesn't seem to work with file data sent using multipart/form-data (ex: from a typical html form).
Ok, sorry I use this in my upload/download code to upload multiple files and use a progress bar (to a client program, not another web-page).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.