加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – HttpPostedFileBase总是在ASP.NET MVC中返回null

发布时间:2020-12-15 19:08:14 所属栏目:asp.Net 来源:网络整理
导读:我有一个问题,当我上传文件在ASP.NET MVC。 我的代码如下: 视图: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}h2Index2/h2@using (Html.BeginForm("FileUpload","Board",FormMethod.Post,new { enctype = "multipart/form-da
我有一个问题,当我上传文件在ASP.NET MVC。
我的代码如下:

视图:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index2</h2>
@using (Html.BeginForm("FileUpload","Board",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
  <input type="file" />
  <input type="submit" />
}

控制器:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
    if (uploadFile != null && uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(Server.MapPath("/Temp"),Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

但uploadFile总是返回null。
谁能弄明白为什么?

解决方法

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index2</h2>
@using (Html.BeginForm("FileUpload",new { enctype = "multipart/form-data" }))
{
  <input type="file" name="uploadFile"/>
  <input type="submit" />
}

您必须提供输入类型文件的名称到uploadfile以便在ASP.net mvc中建模绑定工作,并确保输入类型文件的名称和HttpPostedFileBase的参数名称相同。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读