I am trying to submit a form using ajax. Form contains text fields and a file upload field. The problem is that it submits the text but it does not submit files. my form is
<form onsubmit ="return save();" id="postadform" enctype="multipart/form-data">
Name<input type ="text" name ="name" />
Upload image <input type="file" name="image" id ="filee"/>
<input type ="submit" value = "submit" />
</form>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
function save() {
$.ajax({
type: 'POST', url: '/Home/saveData', data: $('#postadform').serialize(),enctype:"multipart/form-data",
success: function (x) {
//do what ever you want
},
error: function () {
alert('There is some error, plz try again!');
}
});
return false;
}
</script>
And this is part of the HomeController.cs file:
[HttpPost]
public String saveData()
{
String name = Request["name"];
String filename = Request.Files[0].FileName; //Problem in this line.
return "Successful";
}