title: how to send upload control data to jquery ajax wihout plugins
body: <p>I am trying to upload file/files to server through Ajax request in MVC, but the file is always returning null in the controller, can you please suggest the way to pass the file data in the ajax request?</p>
  
  <pre><code>&lt;form method="post" enctype="multipart/form-data"&gt;
  &lt;div&gt;
      @Html.AntiForgeryToken()
      &lt;input type="file" name="file" id="files" multiple&gt;&lt;br&gt;
      &lt;input type="button" value="Upload File to Server" id="submit"&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  
  &lt;script type="text/javascript"&gt;
  $("#submit").click(function () {
          var formData = $('#files').val();
          alert(formData);
          $.ajax({
              url: 'home/index',
              type: 'POST',
              datatype: 'json',
              enctype: "multipart/form-data",
              data: { file: formData },
              //processData: false, // Don't process the files
              //contentType: false, // Set content type to false as jQuery will tell the server its a query string request
              success: function (data) {
                  alert(data);
              },
              error: function () {
                  alert("failed");
              }
          });
      });
  &lt;/script&gt;
  
  
  [HttpPost]
      [ActionName("Index")]
      public ActionResult Index_Post(HttpPostedFileBase file)
      {
          string errormsg = string.Empty;
          if (file != null)
          {
              // Verify that the user selected a file
              if (file != null &amp;&amp; file.ContentLength &gt; 0)
              {
                  // extract only the fielname
                  var fileName = Path.GetFileName(file.FileName);
                  // TODO: need to define destination
                  var path = Path.Combine(Server.MapPath("~/Upload"), fileName);
                  try
                  {
                      file.SaveAs(path);
                      errormsg = "uploaded";
                      return Json(fileName + errormsg);
                  }
                  catch
                  {
                      errormsg = "failed to upload";
                      return Json(fileName + errormsg);
                  }
              }
          }
  
          else
          {
              errormsg = "No file selected";
              return Json(errormsg);
          }
          return View();
      }   
  }
  </code></pre>