MVC上传图片.
uploadify 3.2 Jquery插件 功能强大,但是浏览器不兼容 也是一大问题,在鼓捣上传插件1天后,后来决定,不用其插件。
用最原始的方法,获取文件, 废话不多说,uploadify 网上有大量的教程,在此就不重复了。如果需要,请看下一篇uploadify 3.2 Jquery插件介绍
下面看MVC View里面
1 @using (@Html.BeginForm("ApplyForTeacher", "Customer", FormMethod.Post,new { enctype = "multipart/form-data" })) 2 { 3 4 <dt>身份证照片:<input id="File_upload_0" name="file_upload_CardPictr" type="file" onchange="changeSrc(this)"/></dt> 5 6 <dt>职位证明: <input id="File_upload_1" name="file_upload_1" type="file" onchange="changeSrc(this)" /> </dt> 7 8 <p> </p> 9 <dt><input id="submit" type="submit" onclick="return checkform()" /></dt> 10 }
由于是从做的项目中提取出来的,难免有些删减,如果标签什么的不对,纯属正常现象。
好现在表单创建好了,ApplyForTeacher是Control中的方法,Customer就不用多说了,自然是控制器。(大神勿喷,小弟第一次发,喷也请轻点,纯为新手打造)o(∩_∩)o
下面看Control中的方法:
1 public ActionResult ApplyForTeacher() 2 { 3 4 return View(); 5 } 6 [HttpPost] 7 public ActionResult ApplyForTeacher(Teacher teacher) 8 { 9 Teacher NewTeacher; 10 11 //存身份证照片 12 13 string path = Server.MapPath(@"~/uploadFile/TeachrImgInfo/") + Request.Files["file_upload_CardPictr"].FileName; 14 NewTeacher.CardImgUrl = path;
15 Request.Files["file_upload_CardPictr"].SaveAs(path); 16 DB.Teacher.InsertOnSubmit(NewTeacher); 17 DB.SubmitChanges(); 18 19 20 //存教师证明 21 22 string provePath = Server.MapPath(@"~/uploadFile/TeachrImgInfo/") + Request.Files["file_upload_1"].FileName; 23 24 CertificatePicture Prove = new CertificatePicture(); 25 Prove.ValueURL = provePath; 26 27 Request.Files["file_upload_1"].SaveAs(provePath); 28 29 DB.CertificatePicture.InsertOnSubmit(Prove); 30 DB.SubmitChanges(); 31 32 return View(); 33 }
差不多是这样了,现在可以看到我在后台获取上传来的文件 如需要通过Request.Files["file_upload_CardPictr"] 这个方法就可以获取到身份证的图片,而它本身就有保存文件的方法,所以比较简单。
服务器上面呢 就存path 地址。
upload 的知识就到这了
啰嗦完了。( ^_^ )/~~ 下回见