MVC3 asynchronous form submission upload image implementation

Source: Internet
Author: User
Tags save file

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.