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

时间:2013-11-26 03:59:51

标签: c# asp.net-mvc asp.net-mvc-4 file-upload

我正在尝试在一张表单上上传多张图片

@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>

我正在尝试使用此

处理控制器上的表单
public ActionResult Create(IEnumerable<HttpPostedFileBase> files, RestaurantModel collection)
{
    if (ViewData.ModelState.IsValid)
    {
    }
}

目前控制器上的files签名中没有任何内容。只使用一个文件

时,这似乎很有效
public ActionResult Create(HttpPostedFileBase file, EventsModel collection)

有人可以指示我允许使用一个提交表单上传多个文件吗?

3 个答案:

答案 0 :(得分:18)

您的问题是表单创建了一个post请求,其中包含模型绑定器可以绑定的信息,因为命名约定不正确。

你知道,你有4个文件字段,每个字段都有不同的名称,模型绑定器要正确绑定它们,你的控制器动作签名应如下所示:

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

遵循MCV设计模式最好的选择是使用包含IEnumerable<HttpPostedFileBase>的ViewModel 然后您将为IEnumerable<HttpPostedFileBase>

创建自定义编辑器模板

这样你就可以这样使用它了:

Html.EditorFor(m=>Model.filesUploaded)

并且您的控制器操作如下所示:

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

其他选项包括: 在文件输入字段上使用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>

和这样的控制器动作:

public ActionResult Create(HttpPostedFileBase files)

或使用多个文件字段,但在其名称中对其进行索引:

<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" />

然后你可以使用这样的控制器动作:

public ActionResult Create(IEnumerable<HttpPostedFileBase> files)

答案 1 :(得分:3)

这仅适用于您的文件输入具有索引名称,如files[0]files[1]files[2],...

为了理解模型绑定到列表如何在asp.net mvc中工作,我建议你阅读这篇文章:Model Binding to a List

您甚至不必使用模型绑定来获取文件。在动作中使用Request.Files来获取它们。

public ActionResult Create(EventsModel collection)
{
    var files = Request.Files;
    // rest of the code...
}

答案 2 :(得分:0)

<td>
      <input type="file" name="files" id="files" multiple="multiple" />
      </td>

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