asp.net-mvc-3 – MVC HttpPostedFileBase总是空
发布时间:2020-12-15 19:06:01 所属栏目:asp.Net 来源:网络整理
导读:我需要一些帮助。我正在尝试使用 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=
我需要一些帮助。我正在尝试使用< 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控制器动作没有正确的签名采取文件列表,但我想这是由于一些测试你在做。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net-mvc – Asp.Net Mvc JQuery ajax输入参数
- ASP.NET C# 连接 Oracle数据库增删改查,事务
- asp.net-mvc – Visual studio – 预编译 – 无点
- asp.net-membership – 如何在成员资格提供者上实
- 实体框架 – 带MVC3的EF4 – 我需要存储库模式吗
- asp.net – 使用ModalPopupExtender而不必设置Ta
- ASP.NET MVC 4的用户管理
- asp.net-mvc-3 – 对MVC4 Web API性能问题进行故
- asp.net-mvc – 流畅的验证自定义验证规则
- asp.net – SignalR并不总是准备好后start().don
热点阅读