title: How to set up a Web API controller for multipart/form-data
body: <p>I am trying to figure this out. I was not getting any useful error messages with my code so I used something else to generate something. I have attached that code after the error message.  I have found a <a href="http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2" rel="noreferrer">tutorial</a> on it but I do not know how to implement it with what I have. This is what I currently have:</p>
  
  <pre><code>public async Task&lt;object&gt; PostFile()
      {
          if (!Request.Content.IsMimeMultipartContent())
              throw new Exception();
  
  
          var provider = new MultipartMemoryStreamProvider();
          var result = new { file = new List&lt;object&gt;() };
          var item = new File();
  
          item.CompanyName = HttpContext.Current.Request.Form["companyName"];
          item.FileDate = HttpContext.Current.Request.Form["fileDate"];
          item.FileLocation = HttpContext.Current.Request.Form["fileLocation"];
          item.FilePlant = HttpContext.Current.Request.Form["filePlant"];
          item.FileTerm = HttpContext.Current.Request.Form["fileTerm"];
          item.FileType = HttpContext.Current.Request.Form["fileType"];
  
          var manager = new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext()));
          var user = manager.FindById(User.Identity.GetUserId());
  
          item.FileUploadedBy = user.Name;
          item.FileUploadDate = DateTime.Now;
  
          await Request.Content.ReadAsMultipartAsync(provider)
           .ContinueWith(async (a) =&gt;
           {
               foreach (var file in provider.Contents)
               {
                   if (file.Headers.ContentLength &gt; 1000)
                   {
                       var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                       var contentType = file.Headers.ContentType.ToString();
                       await file.ReadAsByteArrayAsync().ContinueWith(b =&gt; { item.FilePdf = b.Result; });
                   }
  
  
               }
  
  
           }).Unwrap();
  
          db.Files.Add(item);
          db.SaveChanges();
          return result;
  
      }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Object {message: "The request entity's media type 'multipart/form-data' is not supported for this resource.", exceptionMessage: "No MediaTypeFormatter is available to read an obje…om content with media type 'multipart/form-data'.", exceptionType: "System.Net.Http.UnsupportedMediaTypeException", stackTrace: " at System.Net.Http.HttpContentExtensions.ReadAs…atterLogger, CancellationToken cancellationToken)"}exceptionMessage: "No MediaTypeFormatter is available to read an object of type 'HttpPostedFileBase' from content with media type 'multipart/form-data'."exceptionType: "System.Net.Http.UnsupportedMediaTypeException"message: "The request entity's media type 'multipart/form-data' is not supported for this resource."stackTrace: " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable<code>1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) 
    ↵ at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable</code>1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) </p>
  </blockquote>
  
  <p>Code used to generate error message:</p>
  
  <pre><code>    [HttpPost]
      public string UploadFile(HttpPostedFileBase file)
      {
  
          if (file.ContentLength &gt; 0)
          {
              var fileName = Path.GetFileName(file.FileName);
              var path = Path.Combine(HttpContext.Current.Server.MapPath("~/uploads"), fileName);
              file.SaveAs(path);
  
  
          }
          return "/uploads/" + file.FileName;
      }
  </code></pre>
  
  <p>Class:</p>
  
  <pre><code>public class File
  {
      public int FileId { get; set; }
      public string FileType { get; set; }
      public string FileDate { get; set; }
      public byte[] FilePdf { get; set; }
      public string FileLocation { get; set; }
      public string FilePlant { get; set; }
      public string FileTerm { get; set; }
      public DateTime? FileUploadDate { get; set; }
      public string FileUploadedBy { get; set; }
  
      public string CompanyName { get; set; }
      public virtual ApplicationUser User { get; set; }
  }
  </code></pre>