MVC 4 Beta with Mobile Project FIle Upload does not work

Posted by Jim Shaffer on Stack Overflow See other posts from Stack Overflow or by Jim Shaffer
Published on 2012-05-17T16:33:50Z Indexed on 2012/12/19 17:03 UTC
Read the original article Hit count: 323

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>

© Stack Overflow or respective owner

Related posts about file

Related posts about application