Take the 2-minute tour ×

Testing out Web API for file uploading, have a simple view model like this:

public class TestModel {
    public string UserId {get;set;}
    public HttpPostedFileBase ImageFile {get;set;}

Used in the method:

public void Create(TestModel model)

When I attempt to post a multipart/form-data encoded form to the action, I receive this exception:

System.InvalidOperationException: No MediaTypeFormatter is available to read an object of type 'TestModel' from content with media type 'multipart/form-data'.
   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
   at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
   at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken) 

This works with the default MVC model binder, but apparently not with Web API's. Found some mentions that you can't use a view model when uploading a file, and to just separate the data into two calls. That doesn't work for me, since I need the other fields being posted in order to actually do something with the uploaded file. Is there a way to accomplish this?

share|improve this question
You need to write a custom MediaTypeFormatter to make it work. As you experienced "multipart/form-data" is not supported out of the box. You can start here –  nemesv Oct 2 '12 at 20:05

2 Answers 2

up vote 2 down vote accepted

See my original answer http://stackoverflow.com/a/12603828/1171321

Basically combine my method in my blog post and the TryValidateProperty() suggestion to maintain model validation annotations.

Edit: I went ahead and worked up a code enhancement to my code in the blog post. I'm going to post this updated code shortly. Here is a simple example that validates each property and gives you access to an array of the results. Just a sample of one approach

public class FileUpload<T>
        private readonly string _RawValue;

        public T Value { get; set; }
        public string FileName { get; set; }
        public string MediaType { get; set; }
        public byte[] Buffer { get; set; }

        public List<ValidationResult> ValidationResults = new List<ValidationResult>(); 

        public FileUpload(byte[] buffer, string mediaType, string fileName, string value)
            Buffer = buffer;
            MediaType = mediaType;
            FileName = fileName.Replace("\"","");
            _RawValue = value;

            Value = JsonConvert.DeserializeObject<T>(_RawValue);

            foreach (PropertyInfo Property in Value.GetType().GetProperties())
                var Results = new List<ValidationResult>();
                                              new ValidationContext(Value) {MemberName = Property.Name}, Results);

        public void Save(string path, int userId)
            if (!Directory.Exists(path))
            var SafeFileName = Md5Hash.GetSaltedFileName(userId,FileName);
            var NewPath = Path.Combine(path, SafeFileName);
            if (File.Exists(NewPath))

            File.WriteAllBytes(NewPath, Buffer);

            var Property = Value.GetType().GetProperty("FileName");
            Property.SetValue(Value, SafeFileName, null);
share|improve this answer

You can either write a custom MediaTypeFormatter to facilitate your scenario or you can pull the data out of the request by hand using MultipartFormDataStreamProvider.FormData.AllKeys collection. This way you can post both the file(s) and additional fields in one request.

A good tutorial by Mike Wasson is available here: http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

share|improve this answer
So after reading the article, I see how this can be done. However, unless I'm misunderstanding, there's still no way to use the model binder (and thus all of the various data annotations that go with it). So we now have to validate all of the input manually? Do you by chance know why this change was necessary in the model binder? It quite effectively breaks the notion of being able to use existing models for API projects, and seems like a really strange direction to take. –  heyseuss Oct 3 '12 at 13:16

Your Answer


