I'm trying to use Ajax.BeginForm with HttpPostedFileBase to upload files. In trying to trace the issue I'm using the rendered version of the form element.
I'm using the exact same code in 2 applications. A dummy program which has nothing but this code, and the actual application in which I want to embed the file upload code. In the dummy app this ajax code works perfectly sending the file to the controller. In the actual corporate application, this code works only if the data-ajax="true" is left off, otherwise AttachmentFile is null. So, it works as an html form submit. It doesn't work as an ajax submit.
Can anyone give me a clue as to why HttpPostFileBase works with Ajax in my dummy mvc solution, but not in my full blown corporate application?
I've spent a couple of days in stack overflow and trying different things in my application. Nothing has worked. So I'm asking for help, if somebody has experience where a setting in a project stops HttpPostFileBase from working with Ajax.
_layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/app")
@RenderSection("scripts", required: false)
</head>
<body>
<div>
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/app")
@Scripts.Render("~/bundles/modernizr")
</body>
</html>
Index.cshtml
@{
ViewBag.Title = "Web6 Home Page";
}
<form id="form0" action="/Home/Index" enctype="multipart/form-data" method="post" novalidate="novalidate" data-ajax="true" >
<input name="AttachmentFile" type="file" />
<input id="FileUploadBtn" type="submit" value="Upload" />
</form>
Home.cs
[HttpPost]
public ActionResult Index(HttpPostedFileBase AttachmentFile)
{
//if (AttachmentFile.ContentLength > 0)
//{
// var fileName = Path.GetFileName(AttachmentFile.FileName);
// var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
// AttachmentFile.SaveAs(path);
//}
return RedirectToAction("Index"
}