sienteasturias
New Member
I want to upload file using ajax to an ASP.Net MVC ControllerMy JavaScript code looks like this, basically every time the user changed the input value, it will automatically upload the file to the controller\[code\]var formdata = http://stackoverflow.com/questions/12799127/false;if (window.FormData) { formdata = new FormData();}$('input[name=default_export_filename]').change(function() { var i = 0, len = this.files.length, img, reader, file; for ( ; i < len; i++ ) { file = this.files; if (file.type.match(/spreadsheet/) || file.type.match(/ms-excel/)) { if ( window.FileReader ) { reader = new FileReader(); reader.onloadend = function (e) { //showUploadedItem(e.target.result, file.fileName); }; reader.readAsDataURL(file); } if (formdata) { formdata.append("default_export_filename", file); } } } if (formdata) { $.ajax({ url: root + mod + '/uploaddef', type: "POST", data: formdata, processData: false, contentType: false, success: function (res) { console.log(res); } }); }});\[/code\]In the controller side, I couldn't catch that file using this\[code\] [HttpPost] public ActionResult UploadDEF(HttpPostedFileBase file) { var jsonData = http://stackoverflow.com/questions/12799127/new { response = 3 }; return Json(jsonData); ; }\[/code\]Or this\[code\] [HttpPost] public ActionResult UploadDEF(FormCollectionfile) { var jsonData = http://stackoverflow.com/questions/12799127/new { response = 3 }; return Json(jsonData); ; }\[/code\]The file argument is always null. But in the firebug, I can see that the browser is posting the file to the server. With what object should I catch the file?I realize the FormData object is not supported in older browser, but my clients are all using the latest browser. So that is not a problem.