title: Ajax post large base64 string to Web Api fails
body: <p>I send an ajax post to a controller that contains a base64 encoded string along with some other data objects.
  For now, I have a simple controller that writes a static image to a folder. </p>
  
  <p>Code below</p>
  
  <pre><code>  [System.Web.Http.Route("api/Assessment/saveLvlTAssessment1")]
          [System.Web.Http.HttpPost]
          [EnableCors("*", "*", "*")]
          public HttpResponseMessage SaveAssessment1([FromBody] MobileLvlTAssessmentModel model)
          {
              var response = new HttpResponseMessage();
              String path = System.Web.Hosting.HostingEnvironment.MapPath(WebConfigurationManager.AppSettings["ImagePath"]); //Path
              string image = model.Image;
              try
              {
                  string fileName = path + "/Test#@@#.jpg";
  
                  byte[] imageBytes = Convert.FromBase64String(image);
                  using (FileStream
                      fileStream = new FileStream(fileName, FileMode.Create))
                  {
                      // Write the data to the file, byte by byte.
                      for (int i = 0; i &lt; imageBytes.Length; i++)
                      {
                          fileStream.WriteByte(imageBytes[i]);
                      }
  
                      // Set the stream position to the beginning of the file.
                      fileStream.Seek(0, SeekOrigin.Begin);
                  }
              }
  
  
              catch (Exception ex)
              {
                  response = Request.CreateResponse(HttpStatusCode.Accepted, ex.ToString());
                  throw ex;
              }
  
              if (response.Content == null)
              {
                  response = Request.CreateResponse(HttpStatusCode.Accepted, "Saved");
              }
              return response;
          }
  </code></pre>
  
  <p>My ajax call is like this</p>
  
  <pre><code>  SaveLvlTAssessment: function (data) {
              var deferred = q.defer();
              window.app.ajaxSetUp()
                  .then(function (authdata) {
                      $.ajax({
                          url: window.routeDictionary.incident.SaveLvlTAssessment,
                          type: "POST",
                          contentType: "application/json",
                          dataType: "json",
                          data: JSON.stringify(data),
                          beforeSend: function (request) {
                              request.setRequestHeader("Authorization", "Basic " + authdata);
                          }
                      }).then(function (data, textStatus, jqXHR) {
                          delete jqXHR.then; // treat xhr as a non-promise
                          deferred.resolve(data);
                      }, function (jqXHR, textStatus, errorThrown) {
                          helperPlugins.removeWait();
                          deferred.reject(new Error(jqXHR));
                      });
                  });
              return deferred.promise;
          }
  </code></pre>
  
  <p>I have added this to my web.config</p>
  
  <pre><code>&lt;security&gt;
        &lt;requestFiltering&gt;
      &lt;requestLimits maxAllowedContentLength="10485760" /&gt;
        &lt;/requestFiltering&gt;
              &lt;authentication&gt;
                  &lt;anonymousAuthentication userName="dot\achatterjee" password="[enc:AesProvider:ojOlpPI0WUJNmGMlrVoBzBqwj4XpXW8cX2vVI9KY5QZLLOfxJ/inyx8ZR6fVSo5S:enc]" /&gt;
                  &lt;windowsAuthentication enabled="false" /&gt;
              &lt;/authentication&gt;
      &lt;/security&gt;
  </code></pre>
  
  <p>Any my global.asax.cs has this</p>
  
  <pre><code>GlobalConfiguration.Configuration.Formatters.FormUrlEncodedFormatter.ReadBufferSize = 256 * 1024; // 256 KB
  </code></pre>
  
  <p>My post request is consistently failing in the remote server. However, if I dont send the image data it goes through. </p>
  
  <p>Been splitting hair on this for several hours, will appreciate any ideas or hints.</p>