the model:
public class UploadFileModel
{
public int Id { get; set; }
public string FileName { get; set; }
public HttpPostedFileBase File { get; set; }
}
the controller:
public void Post(UploadFileModel model)
{
// never arrives...
}
I am getting an error
"No MediaTypeFormatter is available to read an object of type 'UploadFileModel' from content with media type 'multipart/form-data'."
Is there anyway around this?
It's not easily possible. Model binding in Web API is fundamentally different than in MVC and you would have to write a MediaTypeFormatter that would read the stream of files into your model and additionally bind primitives which can be considerably challenging.
The easiest solution is to grab the file stream off the request using some type of
MultipartStreamProvider
and the other parameters usingFormData
name value collection off that providerExample - http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2: