代码如下:
<form action="~/Home/Indexs" method="post" enctype="multipart/form-data"> <label for="file1">Filename:</label> <input type="file" name="files1" id="file1" /> <input type="submit" /> </form>
后台
[HttpPost] public ActionResult Indexs(HttpPostedFileBase[] files) { var t=Request.Files[0]; if (files != null) { DateTime time = DateTime.Now; foreach (var file in files) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/uploads/" + time.ToString()), fileName); file.SaveAs(path); } } } return RedirectToAction("Index"); }
单步调试files参数为空、request.Files的Count为0
..
<input type="file" name="files1" id="file1" />的name要一直
或者Request.Files里面可以获取文件