mvc中上传图片到指定文件夹中
前台:

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 }
作者:魔女小溪
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.