我正在使用ASP.NET MVC-5,我有一个按钮,可以让我选择多个文件进行输入,所以它适用于这段代码
<input type="file" name="file" multiple>
如何从控制器的[HttpPost]动作中获取文件以在我的程序逻辑中使用它们?
答案 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();
}