简体   繁体   English

以一种形式MVC4上传多个文件

[英]Upload multiple files in one form MVC4

I'm trying to upload multiple images on one form 我正在尝试在一个表单上上传多个图像

@using (Html.BeginForm("Create", "AdminRestaurants", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
    <label for="logoFile" class="col-sm-2 control-label">Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="logoFile" id="logoFile" />
    </div>
</div>
<div class="form-group">
    <label for="fohFile" class="col-sm-2 control-label">FOH Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="fohFile" id="fohFile" />
    </div>
</div>
<div class="form-group">
    <label for="bohFile" class="col-sm-2 control-label">BOH Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="bohFile" id="bohFile" />
    </div>
</div>
<div class="form-group">
    <label for="mgmFile" class="col-sm-2 control-label">MGM Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="mgmFile" id="mgmFile" />
    </div>
</div>

I'm trying to process the form on the controller with this 我正试图用这个处理控制器上的表单

public ActionResult Create(IEnumerable<HttpPostedFileBase> files, RestaurantModel collection)
{
    if (ViewData.ModelState.IsValid)
    {
    }
}

Currently nothing shows up in the files signature on the controller. 目前,控制器上的files签名中没有任何内容。 This seems to work great when only working with one file 仅使用一个文件时,这似乎很有效

public ActionResult Create(HttpPostedFileBase file, EventsModel collection)

Can someone point me in the direction to allow multiple files to be uploaded with one submission form? 有人能指出我允许用一个提交表单上传多个文件的方向吗?

Your problem is that you form creates a post request with information that the model binder can bind because the naming convention is not right. 您的问题是表单创建了一个post请求,其中包含模型绑定器可以绑定的信息,因为命名约定不正确。

you see, you have 4 file fields each with a different name, for the model binder to bind them correctly your controller action signature should look like this: 你看,你有4个文件字段,每个字段都有不同的名称,模型绑定器正确绑定它们你的控制器动作签名应如下所示:

public ActionResult Create(HttpPostedFileBase mgmFile, 
                           HttpPostedFileBase logoFile, 
                           HttpPostedFileBase fohFile ,
                           HttpPostedFileBase bohFile)

Following MCV design pattern The best option would be to use a ViewModel that holds an IEnumerable<HttpPostedFileBase> and you would create a custom editor template for an IEnumerable<HttpPostedFileBase> 继MCV的设计模式最好的办法是使用一个保存了一个视图模型IEnumerable<HttpPostedFileBase>你会为创建一个自定义编辑器模板IEnumerable<HttpPostedFileBase>

so that you could use it like that: 所以你可以像这样使用它:

Html.EditorFor(m=>Model.filesUploaded)

and your controller action would look like this: 你的控制器动作看起来像这样:

public ActionResult Create(MyViewModel i_InputModel)
{
    i_InputModel.filesUploade; //Use the files here to upload them.
}

Other options are: Use the HTML5 multiple attribute on the file input field like this: 其他选项包括:在文件输入字段上使用HTML5 multiple属性,如下所示:

<label for="mgmFile" class="col-sm-2 control-label">Files:</label>
<div class="col-sm-6">
    <input type="file" multiple="multiple" name="files" id="files" />
</div>

and a controller action like this: 和这样的控制器动作:

public ActionResult Create(HttpPostedFileBase files)

or use multiple file fields but index them in their name: 或使用多个文件字段,但在其名称中对其进行索引:

<input type="file" multiple="multiple" name="files[0]" id="files_1" />
<input type="file" multiple="multiple" name="files[1]" id="files_2" />
<input type="file" multiple="multiple" name="files[2]" id="files_3" />
<input type="file" multiple="multiple" name="files[3]" id="files_4" />

and then you could use a controller action like this: 然后你可以使用像这样的控制器动作:

public ActionResult Create(IEnumerable<HttpPostedFileBase> files)

This would only work if your file inputs had indexed names like files[0] , files[1] , files[2] ,... 只有当您的文件输入具有索引名称(如files[0]files[1]files[2] ,... files[2] ,这才有效。

In order to understand how model binding to a list works in asp.net mvc, I recommend that you read this post: Model Binding to a List 为了理解模型绑定到列表如何在asp.net mvc中工作,我建议你阅读这篇文章: 模型绑定到列表

You don't even have to use model binding to get the files. 您甚至不必使用模型绑定来获取文件。 Use Request.Files in your Action to get them. 在Action中使用Request.Files来获取它们。

public ActionResult Create(EventsModel collection)
{
    var files = Request.Files;
    // rest of the code...
}
<td>
      <input type="file" name="files" id="files" multiple="multiple" />
      </td>

As Here, I demonstrate with simple example : http://www.infinetsoft.com/Post/How-to-create-multiple-fileUpload-using-asp-net-MVC-4/1229#.V0J-yfl97IU 在这里,我用简单的例子演示: http//www.infinetsoft.com/Post/How-to-create-multiple-fileUpload-using-asp-net-MVC-4/1229#.V0J-yfl97IU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM