MVC 4 Beta with Mobile Project FIle Upload does not work
- by Jim Shaffer
I am playing around with the new MVC 4 beta release. I created a new web project using the Mobile Application template. I simply added a controller and a view to upload a file, but the file is always null in the action result. Is this a bug, or am I doing something wrong?
Controller Code:
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace MobileWebExample.Controllers
{
public class FileUploadController : Controller
{
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(HttpPostedFileBase file)
{
int i = Request.Files.Count;
if (file != null)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
}
}
And the view looks like this:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form action="@Url.Action("Upload")" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" value="Submit" />
</form>