Since the project requires "any one can upload an indefinite picture", these two days are writing this feature. Now it's done, record it.
Language: C #
Environment: MVC3 + EF4
Required Plugins: http://download.csdn.net/detail/tl110110tl/8248099
The required database tables are as follows:
Plugin reference: http://www.cnblogs.com/china-li/archive/2012/12/12/2800144.html
HTML code:
@{Layout = null;} <! DOCTYPE html>
Controller for background
Namespace SGS. mainproto.controllers{public class Skincontroller:controller {////GET:/SKIN/IBLL. ISKINBLL _SKINBLL = new BLL. SKINBLL (); Public ActionResult Index () {return View (); }///<summary>//Add skin///Url:/skin/addskin//</summary> public Act Ionresult Addskin () {return View (); }///<summary>//Add Pictures///URL:/SKIN/ADDIMG///</summary> [HttpPost] Public ActionResult addimg () {String TypeID = request["TypeID"]; int typeId; if (!int. TryParse (TypeID, out TypeID)) {return Content ("Error_typeid error"); } httppostedfilebase file = Request.files[0]; if (file. ContentLength > 0 && file. contentlength/1024 < 10000) {//Get the file extension string ext = System.IO.Path.GetExtension (file. FileName); if (ext. ToLower () = = ". jpg" | | Ext. ToLower () = = ". png" | | Ext. ToLower () = = ". gif") {string datepath = DateTime.Now.ToString ("Yyyy/mm/dd");//Time path String onefilepath = Server.MapPath ("/imageup/" + datepath);//Time Local path if (! System.IO.Directory.Exists (Onefilepath))//Create local directory {System.IO.Directory.CreateDir Ectory (Onefilepath); } string filename = Guid.NewGuid (). ToString () + ext;//unique save file name string Finallfilepath = Onefilepath + "/" + filename;//final Local save path Model.skin Skin = new Model.skin (); Skin. Skintype = Typeid.tostring (); Skin. Skinname = file. FileName; Skin. Skinurl = "/imageup/" + Datepath + "/" + filename; _skinbll.addentity (skin); File. SaveAs (Finallfilepath); var skinurl = _skinbll.loadentitys (S=>s.skintype==typeid). OrderByDescending (s=>s.id). Select (s=>s.skinurl+ "|" +s.skinname); string res = ""; foreach (string item in Skinurl) {res + = Item + ","; } res = Res. TrimEnd (', '); Return Content (RES); } else {return Content ("Error_ is not a picture file"); }} else {return Content ("The Error_ file is empty or exceeds 8M"); }} [HttpPost] public ActionResult Getimgurl () {String TypeID = request["TypeID"]; if (string. IsNullOrEmpty (TypeID)) {return Content ("Error_typeid is empty"); } var Skinurl = _skinbll.loadentitys (s = = S.skintype = = TypeID). OrderByDescending (s = = s.id). Select (S => S.skinurl + "|" + S.skinname); string res = ""; foreach (string item in Skinurl) {res + = Item + ","; } res = Res. TrimEnd (', '); Return Content (RES); } }}
MVC3 asynchronous form submission upload image implementation