I am trying to upload a file with ASP.NET MVC.
The following code work perfectly fine:
// Read in the image data.
byte[] binaryData = null;
HttpPostedFil
Not sure if this is relevant to the question, but I've just found a way to get model binding for HttpPostedFileBase working, within complex objects. Unfortunately I had to make a change to the ASP.NET MVC source code, so it isn't for everybody.
In System.Web.Mvc.ValueProviderDictionary.PopulateDictionary(), the value provider is being populated with the contents of Request.Form, Request.QueryString, and RouteData - but NOT Request.Files. Add the following lines to "fix" this (I say "fix" because there may be a reason the ASP.NET MVC team didn't do it in the first place).
HttpFileCollectionBase files = ControllerContext.HttpContext.Request.Files;
if (files != null)
{
string[] keys = files.AllKeys;
foreach (string key in keys)
{
HttpPostedFileBase file = files[key];
ValueProviderResult result = new ValueProviderResult(file, file.FileName, currentCulture);
AddToDictionaryIfNotPresent(key, result);
}
}