MVC 4 Beta with Mobile Project FIle Upload Not Working

I am playing with the new MVC 4 beta. I created a new web project using the Mobile Application template. I just added a controller and a view to load the file, but the file always has a null 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>

      

+1


source to share


1 answer


I had a similar problem, albeit not with a cell phone.

Replace

public ActionResult Upload(HttpPostedFileBase file)

      



from

public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)

      

Then you can access the file from the collection

0


source







All Articles