Asp.net MVC: upload multiple image files?

前端 未结 4 429
礼貌的吻别
礼貌的吻别 2020-12-08 05:19

is there a good example of how to upload multiple image files in asp.net mvc? I know we can use HttpPostedFileBase to upload one file. Is there a way to upload multiple file

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 06:04

    You could implement an action with POST http verb to that receive a collection of HttpPostedFileBase and save all files, for sample:

    [HttpPost]
    public ActionResult Upload(IEnumerable files) 
    {
        foreach (var file in files)
        {
            file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
        }
    
        return View();
    }
    

    Alternatively, you could read Request.Files and do the same job,

    [HttpPost]
    public ActionResult Upload() 
    {
        foreach (var file in Request.Files)
        {
            file.SaveAs(Server.MapPath("~/Update/" + file.FileName));
        }
    
        return View();
    }
    

    See Also

    • Single File Upload to Multiple File Upload in MVC
    • Uploading a File (Or Files) With ASP.NET MVC

提交回复
热议问题