views:

320

answers:

2

I want to use the HttpPostedFile Class to upload one or more large files to an ASP.NET MVC controller from a web page. Using this class, uploaded files larger than 256 KB are buffered to disk, rather than held in server memory.

My understanding is that it can be done like this:

if (context.Request.Files.Count > 0)
{
    string tempFile = context.Request.PhysicalApplicationPath;
    for(int i = 0; i < context.Request.Files.Count; i++)
    {
        HttpPostedFile uploadFile = context.Request.Files[i];
        if (uploadFile.ContentLength > 0)
        {
            uploadFile.SaveAs(string.Format("{0}{1}{2}",
              tempFile,"Upload\\", uploadFile.FileName));
        }
    }
}

Is there a way to set a callback, or using some other method, return status periodically to the web page via AJAX or JSON so that a progress bar and percent completed can be displayed? What would the code look like?

+2  A: 

Nope. Asp.net always load the whole of the request content as soon as you use HttpRequest.InputStream.

If you want to provide such a feedback, you either need to do it on the client-side using something like flash, or write your own http hanlder that would go straight to the HttpWorkerRequest methods to load the entity body yourself.

serialseb
A: 

I tend to agree with serialseb. You will need to write your own http handler which maybe outputs upload progress to some event which maybe with a specific action for querying such progress', allows the client to query for updates periodically.

cottsak