Hi everyone,
I ran into the following error while trying to get image uploads working with Web API 2:
ExceptionMessage=No MediaTypeFormatter is available to read an object of type ‘HttpPostedFileBase’ from content with media type ‘multipart/form-data’.
I had been trying to copy the following from an mvc controller in another project:
public IHttpActionResult Upload(HttpPostedFileBase file, Models.Image.ImageAssociationType associationType, int associationId)
The fix was to use the following instead:
public IHttpActionResult Upload(Models.Image.ImageAssociationType associationType, int associationId)
{
var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
…
{
var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
…
Thanks to this stackoverflow post for the info: https://stackoverflow.com/a/28370156/522859
Leave a Reply