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控制器动作没有正确的签名采取文件列表,但我想这是由于一些测试你在做。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – mvc3剃刀条件包装器div
- asp.net-mvc – ASP.NET MVC – 如何获取一个URL而不是一个
- asp.net-mvc – 使用UIHint的ASP.NET MVC 3自定义显示模板
- .net – 使用log4net和在哪里实现它并使用elmah?
- asp.net-mvc – ASP.NET MVC – 保持控制器薄(太多的动作方
- asp.net-web-api – 我可以在WebAPI messageHandler中使用A
- 为什么在ASP.Net中向StatusDescription添加换行符会关闭连接
- asp.net – 谁在生产应用程序中实际使用DataGrid / GridVie
- asp.net-mvc – HandleErrorAttribute无法正常工作
- asp.net – 什么是连接池?
- asp.net – HttpRuntime Cache和HttpContext Cac
- asp.net-mvc-3 – MVC3:如何在HtmlHelper扩展中
- asp.net – 如何通过ADO.NET运行我的.sql脚本文件
- asp.net-mvc – 使用disabled =“disabled”属性
- asp.net – 成员资格生成密码仅字母数字密码?
- 如何阻止ASP.NET Menu控件生成内联html样式元素
- asp.net-mvc – 如何访问querystring在ASP.Net M
- asp.net-identity – UseOAuthBearerTokens vs U
- asp.net – 异步HttpWebRequest,从Web应用程序中
- asp.net-mvc – DataAnnotationsModelBinder如何
