我需要一些帮助.我正在尝试使用上传文件<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" />
}
Run Code Online (Sandbox Code Playgroud)
这是一个应该处理上传文件的Action.
[HttpPost]
public ActionResult BookAdd(IEnumerable<HttpPostedFileBase> files)
{
// some actions
return View();
}
Run Code Online (Sandbox Code Playgroud)
问题是"文件"总是包含两个空元素.可以做些什么来解决它?
是时候发布一些新闻了.好像我发现了问题,但我仍然不知道如何修复它.看来,尽管我在这里使用"multipart/form-data":
@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, 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" />
}
Run Code Online (Sandbox Code Playgroud)
Request.ContentType
在控制器中仍然是"application/x-www-forum-urlencoded"
Dar*_*rov 31
只需删除输入字段名称中的方括号:
@using (Html.BeginForm("BookAdd", "Admin", FormMethod.Post, 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" />
}
Run Code Online (Sandbox Code Playgroud)
更新:
在查看示例项目后,您发送给我的问题是您有2个嵌套表单.HTML中不允许这样做.您_Layout.cshtml
的BookAdd.cshtml
视图中有一个表单和另一个表单.这就是为什么尽管enctype="multipart/form-data"
你的内在形式属性你得到了错误Request.ContentType
.因此,如果您希望这样做,您将不得不取消这些表格.另外在你发给我的例子中你的BookAdd
控制器动作没有正确的签名文件列表,但我想这是由于你正在做的一些测试.
小智 7
我遇到了同样的问题但在我的情况下,我得到了解决方案.
[HttpPost]
public ActionResult Upload()
{
foreach (string file in Request.Files)
{
fileurl = Request.Files[file];
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
在设计视图中.<%-- <form id="form1" runat="server">--%>
如果使用母版页请注释表格标签...我希望您的问题将得到解决...
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Upload
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%-- <form id="form1" runat="server">--%>
<% using (Html.BeginForm("Upload","Home",FormMethod.Post,new {enctype="multipart/form-data"}))
{ %>
<fieldset>
<legend>Upload File</legend>
<div>
<p>
Select a File: <input type="file" name="FileUpload" />
<input type="submit" value="Upload" />
</p>
</div>
</fieldset>
<% } %>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46664 次 |
最近记录: |