ASP.NET-MVC5输入多个文件

时间:2015-06-06 03:58:08

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

我正在使用ASP.NET MVC-5,我有一个按钮,可以让我选择多个文件进行输入,所以它适用于这段代码

<input type="file" name="file" multiple>

如何从控制器的[HttpPost]动作中获取文件以在我的程序逻辑中使用它们?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以在控制器操作中从HttpPostedFileBase数组中获取上传的文件。

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase[] uploadedfiles)
        {
            try
            {
                // Loop through array fro getting files
                foreach (HttpPostedFileBase file in files)
                {
                    // get current file name
                    string filename = System.IO.Path.GetFileName(file.FileName);
                    //Saving the file in relative path (server folder)
                    file.SaveAs(Server.MapPath("~/Images/" + filename));
                    string filepathtosave = "Images/" + filename;

                    /* code for saving the image into database */

                }

                ViewBag.Message = "File Uploaded successfully.";
            }
            catch
            {
                ViewBag.Message = "Error while uploading the files.";
            }
            return View();
        }