title: RequestEntityTooLarge response from Web Api when trying to upload a file with HttpClient
body: <p>I'm trying to create a web service to upload files by Post with Asp.Net Web Api. These are the implementations for Client and Web Api respectively:</p>
  
  <p>Client:</p>
  
  <pre><code>using (var client = new HttpClient()) {
      client.BaseAddress = new Uri("https://127.0.0.1:44444/");
      using (var content =
         new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) {
         content.Add(new ByteArrayContent(File.ReadAllBytes(filepath)));
         content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
         var response = await client.PostAsync("api/Synchronization", content);
  
         if (response.IsSuccessStatusCode)
             eventLog1.WriteEntry("Synchronization has been successful", EventLogEntryType.Information);
         else 
             eventLog1.WriteEntry(response.StatusCode + ":" + response.ReasonPhrase, EventLogEntryType.Error);
  
        }
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>public class SynchronizationController : ApiController {
  
      public HttpResponseMessage SynchronizeCsv() {
          var task = this.Request.Content.ReadAsStreamAsync();
          task.Wait();
          Stream requestStream = task.Result;
  
          try {
              Stream fileStream = File.Create(HttpContext.Current.Server.MapPath(path));
              requestStream.CopyTo(fileStream);
              fileStream.Close();
              requestStream.Close();
          }
          catch (IOException) {
              return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "A generic error occured. Please try again later.");
          }
  
          HttpResponseMessage response = new HttpResponseMessage();
          response.StatusCode = HttpStatusCode.Created;
          return response;
      }
  }
  </code></pre>
  
  <p>Web.config:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.5" /&gt;
      &lt;httpRuntime targetFramework="4.5" maxRequestLength="2097152" /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;security&gt;
        &lt;requestFiltering&gt;
          &lt;requestLimits maxAllowedContentLength="2147483648" /&gt;
        &lt;/requestFiltering&gt;
      &lt;/security&gt;
      &lt;handlers&gt;
        &lt;remove name="ExtensionlessUrlHandler-Integrated-4.0" /&gt;
        &lt;remove name="OPTIONSVerbHandler" /&gt;
        &lt;remove name="TRACEVerbHandler" /&gt;
        &lt;add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /&gt;
      &lt;/handlers&gt;
    &lt;/system.webServer&gt;
  </code></pre>
  
  <p>I'm trying to upload a file which is 5Mb, and every time I try to call the service, I get a 413 Http Error Request Entity Too Large. If the file is small (e.g. 40kb) everything works fine.</p>
  
  <p>Looking through internet I tried to do several modifications to the web.config, but nothing seems to work properly. What am I doing wrong?</p>