title: File Upload with Additonal Form Data to Web Api from MVC
body: <p>I am trying to upload a file with additional form data and post to Web API via MVC but i couldn't accomplish.</p>
  
  <p><strong>MVC Side</strong></p>
  
  <p>Firstly i got the submitted form at MVC. Here is the action for this.</p>
  
  <pre><code>    [HttpPost]
              public async Task&lt;ActionResult&gt; Edit(BrandInfo entity) {
  
                  try {
                      byte[] logoData = null;
                      if(Request.Files.Count &gt; 0) {
                          HttpPostedFileBase logo = Request.Files[0];
                          logoData = new byte[logo.ContentLength];
                          logo.InputStream.Read(logoData, 0, logo.ContentLength);
                          entity.Logo = logo.FileName;
                          entity = await _repo.Update(entity.BrandID, entity, logoData);
                      }
                      else
                          entity = await _repo.Update(entity,entity.BrandID);
                      return RedirectToAction("Index", "Brand");
                  }
                  catch(HttpApiRequestException e) {
  // logging, etc                   
                      return RedirectToAction("Index", "Brand");
                  }
              }
  </code></pre>
  
  <p>Below code post the Multipartform to Web API</p>
  
  <pre><code>string requestUri = UriUtil.BuildRequestUri(_baseUri, uriTemplate, uriParameters: uriParameters);
              MultipartFormDataContent formData = new MultipartFormDataContent();
              StreamContent streamContent = null;
              streamContent = new StreamContent(new MemoryStream(byteData));            
              streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
                  FileName = "\"" + fileName + "\"",
                  Name = "\"filename\""
              };
              streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
              formData.Add(streamContent);
              formData.Add(new ObjectContent&lt;TRequestModel&gt;(requestModel, _writerMediaTypeFormatter), "entity");
              return _httpClient.PutAsync(requestUri, formData).GetHttpApiResponseAsync&lt;TResult&gt;(_formatters);
  </code></pre>
  
  <p>As you can see i am trying to send file data and object with same <code>MultipartFormDataContent</code>. I couldn't find better way to send my entity as <code>ObjectContent</code>. Also i am using JSON.Net Serializer</p>
  
  <p>Regarding to fiddler, post seems successfull.</p>
  
  <pre><code>PUT http://localhost:12836/api/brand/updatewithlogo/13 HTTP/1.1
  Content-Type: multipart/form-data; boundary="10255239-d2a3-449d-8fad-2f31b1d00d2a"
  Host: localhost:12836
  Content-Length: 4341
  Expect: 100-continue
  
  --10255239-d2a3-449d-8fad-2f31b1d00d2a
  Content-Disposition: form-data; filename="web-host-logo.gif"; name="filename"
  Content-Type: application/octet-stream
  
  GIF89a��L������X�������wW����������xH�U�)�-�k6�������v6�������̥�v�J���������7����V:�=#�ի�I(�xf�$�������
  // byte data
  // byte data
  'pf�Y��y�ؙ�ڹ�(�;
  --10255239-d2a3-449d-8fad-2f31b1d00d2a
  Content-Type: application/json; charset=utf-8
  Content-Disposition: form-data; name=entity
  
  {"BrandID":13,"AssetType":null,"AssetTypeID":2,"Logo":"web-host-logo.gif","Name":"Geçici Brand","Models":null,"SupplierBrands":null}
  --10255239-d2a3-449d-8fad-2f31b1d00d2a--
  </code></pre>
  
  <p><strong>Web API Side</strong></p>
  
  <p>Finally i am catching post at Web API side and trying to parse but i couldn't. Because <code>MultipartFormDataStreamProvider</code>'s <code>FileData</code> and <code>FormData</code> collections are allways empty.</p>
  
  <pre><code>[HttpPut]
          public void UpdateWithLogo(int id) {
              if(Request.Content.IsMimeMultipartContent()) {
                  var x = 1; // this code has no sense, only here to check IsMimeMultipartContent
              }  
  
              string root = HttpContext.Current.Server.MapPath("~/App_Data");
              var provider = new MultipartFormDataStreamProvider(root);
  
              try {
                  // Read the form data.
                   Request.Content.ReadAsMultipartAsync(provider);
  
                   foreach(var key in provider.FormData.AllKeys) {
                       foreach(var val in provider.FormData.GetValues(key)) {
                           _logger.Info(string.Format("{0}: {1}", key, val));
                       }
                   }
  
                  // This illustrates how to get the file names.
                  foreach(MultipartFileData file in provider.FileData) {
                      _logger.Info(file.Headers.ContentDisposition.FileName);
                      _logger.Info("Server file path: " + file.LocalFileName);
                  }               
              }
              catch(Exception e) {
                  throw new HttpApiRequestException("Error", HttpStatusCode.InternalServerError, null);
              }              
          }
  </code></pre>
  
  <p>I hope you can help to find my mistake.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I also realized that, if i comment out <code>StreamContent</code> or <code>ObjectContent</code> and only add StringContent, still i can't get anything from <code>MultipartFormDataStreamProvider</code>.</p>