MVC图片上传

 HTML代码:

1  <form method="post" enctype="multipart/form-data">
2  上传图片:<input type="file" accept=".jpg,.jpeg,.png,.gif"  id="imageUpLoad" name="file"/>
3  <input type="button" id="mibtn" value="上 传" />
4 </form>

js代码:

1  $("#mibtn").click(function () {
2         this.form.setAttribute("action", "@Url.Action("Mproupdate", "CHome")");
3         this.form.submit();
4     });

action:

 1 public ActionResult Mproupdate(HttpPostedFileBase file) {
 2  //转换只取得文件名,去掉路径。 
 3  string fileName = file.FileName;
 4                 if (fileName.LastIndexOf("\\") > -1)
 5                 {
 6                     fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
 7                 }
 8                 //保存到相对路径下。 
 9                 string path = "/images/" + DateTime.Now.ToString("ddmmss") + fileName;
10                 file.SaveAs(Server.MapPath(path));
11  return RedirectToAction("Mpro", "CHome");
12 }

 

posted on 2016-12-14 18:49  fei!  阅读(166)  评论(0编辑  收藏  举报