asp.net-mvc-3 – MVC HttpPostedFileBase总是空

我需要一些帮助。我正在尝试使用< input type =“file”>上传文件。这里是我的视图:
@using (Html.BeginForm("BookAdd","Admin",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files[0]" id="files[0]" />
    <input type="file" name="files[1]" id="files[1]" />
    <input type="submit" value="Upload Book" />
}

这里是一个动作,应该处理上传文件

[HttpPost]
public ActionResult BookAdd(IEnumerable<HttpPostedFileBase> files)
{
    // some actions
    return View();
}

问题是“文件”总是包含两个为null的元素。
可以做什么来解决它?

是时候了一些新闻。似乎我发现了问题,但我还是不知道如何解决它。看来,尽管事实上我在这里使用“multipart / form-data”:

@using (Html.BeginForm("BookAdd",new { enctype="multipart/form-data" }))
{
    <input type="file" name="File" id="file1" />
    <input type="file" name="File" id="file2" />
    <input type="submit" value="Upload Book" />
}

Request.ContentType在控制器中保持“application / x-www-forum-urlencoded”。

解决方法

只需删除输入字段名称中的方括号:
@using (Html.BeginForm("BookAdd",new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="submit" value="Upload Book" />
}

更新:

看看你发送给我的示例项目的问题是,你有2个嵌套的表单。这在HTML中不允许。在您的_Layout.cshtml中有一个表单,在BookAdd.cshtml视图中有另一个表单。这就是为什么尽管在内部形式的enctype =“multipart / form-data”属性,你得到错误的Request.ContentType。所以如果你想要这个工作,你将不得不排除这些形式。另外在示例中,你发送给我的BookAdd控制器动作没有正确的签名采取文件列表,但我想这是由于一些测试你在做。

相关文章

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