File Upload ASP.NET MVC 3.0

后端 未结 21 1270
无人共我
无人共我 2020-11-22 01:09

(Preface: this question is about ASP.NET MVC 3.0 which was released in 2011, it is not about ASP.NET Core 3.0 which was released in 2019)

I want to

21条回答
  •  忘了有多久
    2020-11-22 01:54

    Simple way to save multiple files

    cshtml

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



    @ViewBag.Message }

    Controller

    [HttpPost]
            public ActionResult Index(HttpPostedFileBase[] files)
            {
                foreach (HttpPostedFileBase file in files)
                {
                    if (file != null && file.ContentLength > 0)
                        try
                        {
                            string path = Path.Combine(Server.MapPath("~/Files"), Path.GetFileName(file.FileName));
                            file.SaveAs(path);
                            ViewBag.Message = "File uploaded successfully";
                        }
                        catch (Exception ex)
                        {
                            ViewBag.Message = "ERROR:" + ex.Message.ToString();
                        }
    
                    else
                    {
                        ViewBag.Message = "You have not specified a file.";
                    }
                }
                return View();
            }
    

提交回复
热议问题