Начните с чтения этой записи в блоге . Затем примените его к вашему сценарию:
<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.