Asp.net uploads images, uploads images,
Presumably, many people often need to upload images at work.
Reference this plug-in http://files.cnblogs.com/files/hmyao/jquery-form.js.
Front-end code
<Form data-ajax-success = "AfterUpload" enctype = "multipart/form-data" id = "frm"> <input type = "file" name = "fileBase" value = "" id = "imgUpload" class = "jiangli_0000w"/> <input type = "submit" id = "btnSub" value = "Upload image"/> <span class = "flat_loe "> the maximum size of the uploaded image is 720px, unlimited height </span> </form>
// Upload an image $ ("# btnSub "). click (function () {$ ("# frm "). ajaxSubmit ({url: "/Slide/UploadImage", type: "Post", success: AfterUpload}); return false ;}); function AfterUpload (msg) {if (msg! = "0") {$ ("# imgPath "). removeAttr ("src "). attr ("src", msg);} else {swal ({title: "tip", text: "incorrect image format"}); $ ("# imgPath "). val (""); return false ;}}
The corresponding background code is as follows:
/// <Summary> /// upload an image /// </summary> /// <param name = "fileBase"> </param> /// <returns> </returns> [HttpPost] public ActionResult UploadImage (HttpPostedFileBase fileBase) {string imgurl = string. empty; string imgPath = System. IO. path. getFileName (fileBase. fileName); int index = imgPath. lastIndexOf ('. '); string suffix = imgPath. substring (index ). toLower (); if (suffix = ". jpg "| suffix = ". jpeg "| Suffix = ". png "| suffix = ". gif "| suffix = ". bmp ") {string pictureName = DateTime. now. ticks. toString () + suffix; // image name string savePath = Server. mapPath ("/Files/Images/SlideConfig/"); // if (! Directory. exists (savePath) {Directory. createDirectory (savePath);} imgurl = "http: //" + Request. url. authority + "// Files/Images/SlideConfig/" + pictureName; fileBase. saveAs (savePath + pictureName);} else {imgurl = "0";} return Content (imgurl );}
It is very important that many people cannot obtain the upload PATH value. Note that the parameter name must be the same as the file tag name.
This ends...