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; }
}