MVC 4 Beta with Mobile Project FIle Upload does not work

六眼飞鱼酱① 提交于 2019-12-11 08:21:57

问题


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>

回答1:


I had a similar problem though not with mobile.

Replace

public ActionResult Upload(HttpPostedFileBase file)

with

public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)

You can then access the file from the collection



来源:https://stackoverflow.com/questions/10639756/mvc-4-beta-with-mobile-project-file-upload-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!