从阅读这篇博文开始。然后将其应用于您的场景:
<form action="/Home/CreateAgent" method="post" enctype="multipart/form-data">
<input type="file" name="file1" id="file" />
<input type="file" name="file2" id="file" />
... Some other input fields for which we don't care at the moment
and for which you definetely should create a view model
instead of using FormCollection in your controller action
<input type="submit" />
</form>
用 WebForms 语言翻译的结果是:
<% using (Html.BeginForm("CreateAgent", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
<input type="file" name="file1" id="file" />
<input type="file" name="file2" id="file" />
... Some other input fields for which we don't care at the moment
and for which you definetely should create a view model
instead of using FormCollection in your controller action
<input type="submit" />
<% } %>
进而:
public ActionResult CreateAgent(
// TODO: To be replaced by a strongly typed view model as the
// ugliness of FormCollection is indescribable
FormCollection collection,
HttpPostedFileBase file1,
HttpPostedFileBase file2
)
{
// use file1 and file2 here which are the names of the corresponding
// form input fields
}
如果您有很多文件IEnumerable<HttpPostedFileBase>
,请按照 Haacked 的说明使用。
评论:
- 绝对不要
this.HttpContext.Request.Files
在 ASP.NET MVC 应用程序中使用
- 绝对永远永远永远不要
this.HttpContext.Request.Files[collection["personImage"]]
在 ASP.NET MVC 应用程序中使用。