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

我有一个问题,当我上传文件在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的参数名称相同。

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....