MVC4中的上传文件总是得到null异常

时间:2014-04-12 20:25:10

标签: c# asp.net-mvc-4

我已使用

在视图中上传
<div class="SearchArea">

@using (Html.BeginForm("FileUpload", "Noun", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
     @Html.ValidationSummary();
     <ol>
     <li class="lifile">
     <input type="file" id="fileToUpload" name="file" />
     <span class="field-validation-error" id="spanfile"></span>
     </li>
     </ol>
     <input type="submit" id="btnSubmit" value="Upload" />
}   

</div>

,Fileupload方法看起来像

 [HttpPost]
public ActionResult FileUpload(HttpPostedFileBase input)
{
    if (input.ContentLength > 0)
    {
        var fileName = Path.GetFileName(input.FileName);
        var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
        input.SaveAs(path);
    }

我总是得到输入为null,我的routeconfig看起来像(我用同一个项目编译wcf):

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // For general and WCF Configuration 
    routes.MapRoute(
      "Default",
      "{controller}/{action}/{id}",
      new { controller = "Home", action = "Index", id = UrlParameter.Optional },
      new { controller = "^(?!ClassesWebService).*" });

    routes.Add(new ServiceRoute("ClassesWebService", new WebServiceHostFactory(), typeof(WebService.IClassesWebService)));

任何想法如何解决

1 个答案:

答案 0 :(得分:1)

你的名字不匹配。这里有名称file

<input type="file" id="fileToUpload" name="file" />

此处您的名字为input

public ActionResult FileUpload(HttpPostedFileBase input)

将一个重命名为另一个。