views:

28

answers:

1

Given the following code which is extremely generic, I was hoping someone could tell me a bit about what is going on behind the scenes...

[HttpPost]
public ActionResult Load(Guid regionID, HttpPostedFileBase file)
{
    if (file.ContentLength == 0)
        RedirectToAction("blablabla.....");

    var fileBytes = new byte[file.ContentLength];
    file.InputStream.Read(fileBytes, 0, file.ContentLength);
}

Specifically, is the file completely uploaded to the server before my action method is invoked? Or is it the file.InputStream.Read() method call that causes or rather waits for the entire file to upload. Can I do partial reads on the stream and gain access to the "chunks" of the file as it is uploaded? (If the entire fire is uploaded before my method is invoked then it is all a moot point.)

Can anyone point me to some good info on the inner workings here. Is there any difference between IIS6 or II7 here?

Thanks,

+2  A: 

The while file needs to be sent to the server before the action method is invoked. Quote from the documentation:

Files are uploaded in MIME multipart/form-data format. By default, all requests, including form fields and uploaded files, larger than 256 KB are buffered to disk, rather than held in server memory.

You can specify the maximum allowable request size by accessing the MaxRequestLength property or by setting the maxRequestLength attribute of the httpRuntime Element (ASP.NET Settings Schema) element within the Machine.config or Web.config file. The default is 4 MB.

The amount of data that is buffered in server memory for a request, which includes file uploads, can be specified by accessing the RequestLengthDiskThreshold property or by setting the requestLengthDiskThreshold attribute of the httpRuntime Element (ASP.NET Settings Schema) element within the Machine.config or Web.config file.

Server memory won't be consumed on the server but the file contents will be buffered to disk. Once the client has sent the whole file the ASP.NET pipeline will invoke your controller action and you could read the request stream in chunks and save it to another file which will be the definitive location of the uploaded file. The action cannot be invoked before the file has finished uploading as there might be some other fields in the multipart/form-data that come after the file and they won't be assigned in this case.

Darin Dimitrov