0%

【MVC】MVC上傳圖片

  1. View ```csharp
    @using (Html.BeginForm(“edit”, “group”, FormMethod.Post, new { enctype = “multipart/form-data” }))
    {
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

2. Controller,這邊特別注意<span style="color: blue;">HttpPostedFileBase</span> picture的參數名稱要與View file的name相同 ```csharp
public ActionResult AddExhibitionItem(string name, string sort,string stop ,HttpPostedFileBase picture)
{
string err= string.Empty;
if (ExhibitionUtilFactory.AddItem(name, sort, stop, picture, ref err))
{
return Content("<script>alert('新增成功');window.parent.closeDialog();</script>");
}
else {
TempData["alert"] = err;
return View();
}
}

  1. 如果是多檔上傳的話,View ```csharp
1
2
3
4
5
6
7
8
9

4. Controller ```csharp
public ActionResult AddExhibitionItem(string name, string sort,string stop ,IEnumerable<HttpPostedFileBase> picture)
{
foreach (var file in picture) {
............ }
}