title: Sending FormData object with multidimensional array to controller
body: <p>The requirements for one of my controllers changed, it needed to be able to accept a file in addition to it's previous input types: string, string[] and string[][].</p>
  
  <p>Before the file upload requirement it was just making an AJAX call with the 'application/json' contentType. I spotted that I can implement the file and string posting in a single AJAX call by creating a FormData object, append both the text and the file and have the controller accept HttpPostedFileBase.</p>
  
  <p>Posting the file and string worked fine, the problem I encountered was handling javascript arrays. The previous version could quite easily handle it with JSON.stringify, but following the update that needed to change.</p>
  
  <p>I eventually got it to work but I feel I'm missing a better way to handle populating arrays in the FormData object. Here's how it's currently implemented (fields renamed and content cleaned up for readability):</p>
  
  <p>C#</p>
  
  <pre><code>[HttpPost]
  public ActionResult TestAR(string t1, string t2, string[][] t3,
  string[] t4, HttpPostedFileBase t5)
  {
  }
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>var formData = new FormData();
  formData.append('t1', $('#t1').val());
  formData.append('t2', $('#t2').val());
  $.each(t3, function (key1, value1) {
      $.each(value1, function (key2, value2) {
          formData.append('t3[' + key1 + '][' + key2 + ']', value2);
      });
  });
  $.each(t4, function (key1, value1) {
      formData.append('t4[' + key1 + ']', value1);
  });
  formData.append('t5', file);
  
  var farr = [];
  var valForm = $.ajax({
      url: '@Url.Action("TestAR", "TC", null)',
      type: 'POST',
      data: formData,
      cache: false,
      contentType: false,
      processData: false
  })
  .done(function (result) {
      // ...
  })
  .fail(function (event, xhr, status, exc) {
      // ...
  });
  farr.push(valForm);
  </code></pre>
  
  <p>I feel like the array and the multidimensional array can be handled in a cleaner fashion in the FormData object but I can't figure out how I can do that. Is it possible to clean it up any more?</p>