【问题标题】:How to post an array of files in ASP.NET MVC 3?如何在 ASP.NET MVC 3 中发布一组文件?
【发布时间】:2011-11-26 03:43:17
【问题描述】:

我希望能够以一种形式发布多个文件。我想将这些文件作为文件数组传递。例如我想这样做。

<input type="file" name="files[0]" />
<input type="file" name="files[1]" />
<input type="file" name="files[2]" />

然后我希望能够在控制器中接收这些文件作为数组。这个我试过了。

public ActionResult AddPart(HttpPostedFileBase[] files)

但这不起作用。我用谷歌搜索了它,但我能找到的只是上传一个文件的例子。有谁知道如何使用 MVC3 C# 做到这一点。

【问题讨论】:

    标签: .net asp.net html asp.net-mvc-3 post


    【解决方案1】:

    如果您不仅要上传一个文件,则需要在表单中使用enctype="multipart/form-data"

    @using (Html.BeginForm("", "Client", FormMethod.Post, new {enctype="multipart/form-data"}))
    

    和控制器:

    [HttpPost]
    public ActionResult AddPart(IEnumerable<HttpPostedFileBase> files) 
    

    其他部分都没问题。

    【讨论】:

    • 是否还有其他我可能遗漏的内容,因为您发布的解决方案似乎不起作用。
    【解决方案2】:

    好吧,我遇到了几乎相同的情况。但这适用于嵌套的文件数组。

    使用 IEnumerable 作为数组([ ]) 解决了我的问题。 [] s

    [HttpPost]
    public ActionResult AddPart(IEnumerable<HttpPostedFileBase>[] files)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      相关资源
      最近更新 更多