ASP.NET MVC seems to correctly automatically bind between HTML form’s file input field and HttpPostedFileBase. On the other hand it cannot bind from file input field to byte array..I tried and it issues exception – something about not being able to convert to Base64. I had only the byte array property on my Model classes previously because later on I need it to perform serialization of the object into XML file.
Now I’ve come up with this workaround and it works fine but I am not sure if this will be ok:
[DataContract]
public class Section : BaseContentObject
{
...
[DataMember]
public byte[] ImageBytes;
private HttpPostedFileBase _imageFile;
public HttpPostedFileBase ImageFile
{
get { return _imageFile; }
set
{
_imageFile = value;
if (value.ContentLength > 0)
{
byte[] buffer = new byte[value.ContentLength];
value.InputStream.Read(buffer, 0, value.ContentLength);
ImageBytes = buffer;
ImageType = value.ContentType;
}
}
}
[DataMember]
public string ImageType { get; set; }
}
I think you are letting your Model connect to closely with your Controller. The usual way to do this is:
In this case, there is no need for your Model to be aware of
HttpPostedFileBase, which is strictly an ASP.NET concept.If you need complex binding beyond what the
DefaultModelBindersupplies (which is alot), the usual way is to register specialized ModelBinders inGlobal.asaxand then accept your own Model classes as Action Method arguments, like so:In
Global.asax:This ModelBinder could then, for example, find any file that was posted with the form and bind the contents of that file to the
Dataproperty of yourThing.And in your Controller:
In this Action Method, your
ThingModelBinderwould have handled all binding, making it transparent to both the Controller and the Model.Modifying your actual Model classes to be aware of, and function with, ASP.NET would not be necessary in this case. Your Model classes are, after all, supposed to represent your actual data.