Asp.net uploads images, uploads images,

Source: Internet
Author: User

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...

Related Article

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.