I'm trying to send a file using jQuery to my MVC controller but the action keeps receiving a null HttpPostedFileBase parameter.
HTML:
<input type="file" name="file" id="file" />
<input type="submit" name="submit" id="upload" value="Submit"/>
jQuery:
$(function () {
$('#upload').click(function () {
var data = new FormData($('#file')[0].files[0]);
$.ajax({
url: '@Url.Action("Upload", "Home")',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false
});
});
});
Controller:
[HttpPost]
public virtual ActionResult Upload(HttpPostedFileBase file)
{
// file = null
}
new FormData($('#file')[0].files[0]):
__proto__: FormData
$('#file')[0].files[0]:
lastModified: 1445429215528
lastModifiedDate: Wed Oct 21 2015 14:06:55 GMT+0200 (Central Europe Daylight Time)
name: "Google_Chrome_logo_2011.jpg"
size: 5506
type: "image/jpg"
webkitRelativePath: ""
__proto__: File
I pretty much copied the code from other examples that I found on the internet but somehow it is just not working.