Dot-Net

如何在 ASP.NET MVC 3 中發布一組文件?

  • August 20, 2019

我希望能夠以一種形式發布多個文件。我想將這些文件作為文件數組傳遞。例如,我想這樣做。

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

然後我希望能夠在控制器中接收這些文件作為數組。我試過這個。

public ActionResult AddPart(HttpPostedFileBase[] files)

但這不起作用。我用Google搜尋了它,但我能找到的只是上傳一個文件的例子。有誰知道如何使用 MVC3 C# 做到這一點。

如果您不僅要上傳一個文件,則需要enctype="multipart/form-data"在表單中使用。

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

和控制器:

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

其他部分都正常。

好吧,我遇到了幾乎相同的情況。但那是針對嵌套的文件數組。

使用 IEnumerable 作為數組($$ $$) 解決了我的問題。

$$ $$s

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

引用自:https://stackoverflow.com/questions/7571488