title: Upload a file MVC 4 Web API .NET 4
body: <p>I'm using the version of MVC that shipped with Visual Studio 2012 express.  (Microsoft.AspNet.Mvc.4.0.20710.0)</p>
  
  <p>I assume this is RTM version.  </p>
  
  <p>I've found plenty of examples online which all use this code: </p>
  
  <pre><code>    public Task&lt;HttpResponseMessage&gt; PostFormData()
      {
          // Check if the request contains multipart/form-data.
          if (!Request.Content.IsMimeMultipartContent())
          {
              throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
          }
  
          string root = HttpContext.Current.Server.MapPath("~/App_Data");
          var provider = new MultipartFormDataStreamProvider(root);
  
          // Read the form data and return an async task.
          var task = Request.Content.ReadAsMultipartAsync(provider).
              ContinueWith&lt;HttpResponseMessage&gt;(t =&gt;
              {
                  if (t.IsFaulted || t.IsCanceled)
                  {
                      return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                  }
  
                  // This illustrates how to get the file names.
                  foreach (MultipartFileData file in provider.FileData)
                  {
                      Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                      Trace.WriteLine("Server file path: " + file.LocalFileName);
                  }
                  return Request.CreateResponse(HttpStatusCode.OK);
              });
  
          return task;
      }
  </code></pre>
  
  <p>But this code always ends up in continueWith where <code>t.IsFaulted == true</code>.  The exception reads: </p>
  
  <blockquote>
    <p>Unexpected end of MIME multipart stream. MIME multipart message is not
    complete.</p>
  </blockquote>
  
  <p>Here is my client form. NOthing fancy, I want to do jquery form pluging for ajax upload, but I can't even get this way to work.</p>
  
  <pre><code>&lt;form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" &gt;
      &lt;input type="file" /&gt;
      &lt;input type="submit" value="Upload" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I've read that it is caused by the parser expecting /CR /LF at the end of each message, and that bug has been fixed in June.</p>
  
  <p>What I cannot figure out is, if it was really fixed, why isn't it included this version of MVC 4?  Why do so many examples on the internet tout that this code works when it does not in this version of MVC 4?  </p>