前台:
1 @using (Html.BeginForm("AddImg", "UpFileImg", FormMethod.Post, new { enctype = "multipart/form-data" })) 2 { 3 <input type="file" name="file" /> 4 <input type="submit" value="OK" /> 5 }
后台:
1 [HttpPost] 2 public ActionResult AddImg(HttpPostedFileBase file) 3 { 4 if (file != null && file.ContentLength > 0) 5 { 6 7 var fileName =System.IO.Path.GetFileName(file.FileName); 8 var path = System.IO.Path.Combine(Server.MapPath("/img"), fileName); 9 file.SaveAs(path); 10 } 11 return Content("上传成功"); 12 }