このブログ投稿を読むことから始めます。次に、それをシナリオに適用します。
<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 アプリケーションでは絶対に使用しないでください。