title: uploading a file via ajax along with the rest of the form data
body: <p>I have been searching for a solution for two weeks now on this.  I have a custom form that i have built that is pretty unique.  I need to be able to uplaod the form data along with one or many files that are included.
  One the back end we are running ruby on rails.  The data, in order for it to be accepted needs to be formatted into a specific json object.  Here is my script that is currently working for just the form data...not the file</p>
  
  <pre><code>$("#submit").click(function(e) {  
  e.preventDefault();
  files;
  var data = new FormData();
  $.each(files, function(key, value)
  {
      data.append(key, value);
  });
  
  var uploadJsonObject = { 
    post: {
               title: $("#project-title-inputfield").val(),
               post_type: "",
               //contents_attributes: _.map(files, function(file) { return { piece: file } }),    
               comments_attributes: [{
                 body: $("#post_comments_attributes_body").val()
               }]
      }
  }
  
  
  console.log(uploadJsonObject);
      // Using the core $.ajax() method
  $.ajax({
    url: "/posts",
    data:  uploadJsonObject,
    type: "POST",
    dataType : "json",
    success: function( json, uploadJsonObject ) {
        alert("worked")
    },
    error: function( xhr, status ) {
        alert( "Sorry, there was a problem!" );
        console.log("status" + status);
    }
  });  
  
  
  
  
  });   
  </code></pre>
  
  <p>You will notice that the line</p>
  
  <p>contents_attributes: _.map(files, function(file) { return { piece: file } }), </p>
  
  <p>is commented out.  That is how the files need to be formatted in the object.  The problem is that jquery ajax doesn't support files.  When i uncomment the line to try to include the files, i get a 400 (error). I have tried to use different variations of formData and XMLHttpRequest() to modify this, but i have not been able to find any kind of solution. that modifies the files and allows me to include it into that json object.
  Thanks</p>