Asp.net implements asynchronous File Upload Based on ajaxfileupload. js,

Source: Internet
Author: User

Asp.net implements asynchronous File Upload Based on ajaxfileupload. js,

Front-end code:

/* Modify the Avatar * // upload function _ SC () {$ (". ckfile ").html (" ").css (" color "," #535353 "); $ (" # _ userImgPath "). val (""); var str = $ ("# file "). val (); if ($. trim (str) = "") {$ (". ckfile "cmd.html (" select a file. "2.16.css (" color "," red "); return false;} else {var postfix = str. substring (str. lastIndexOf (". ") + 1 ). toUpperCase (); if (postfix = "JPG" | postfix = "JPEG" | postfix = "PNG" | postfix = "GIF" | postfix = "BMP") {$ ('# showimg '). attr ('src', 'images/loading.gif '). attr ("title", "Uploading... please wait... "); Var path =" Upload/UserImg "; $. ajaxFileUpload ({url: '/Upload. aspx? Path = Upload | UserImg & shape = 100*100 ', secureuri: false, fileElementId: 'file', ype: 'text', success: function (msg) {if (msg. lastIndexOf (path) =-1) {$ (". ckfile ").html(msg).css (" color "," red ");} else {$ ('# showimg '). attr ('src', msg ). attr ("title", "my avatar"); $ ("# _ userImgPath "). val (msg) ;}}) ;}else {$ (". ckfile "cmd.html (" File Format error. ").Css (" color "," red "); return false ;}} background code: using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using SS. upload; using WFC. fenxiao; namespace wanfangcheng {public partial class Upload: BasePage {// file size: 1024 kb private long size = 1024; // file type private string type = ". jpg |. jpeg |. png |. gif |. bmp "; // Save the name strin G name = ""; // save path private string path = @ "Upload/UserImg"; // save size private string shape = "100*100 "; protected void Page_Load (object sender, EventArgs e) {HttpFileCollection files = Request. files; if (files! = Null & files. Count> 0) {name = BaseRole. Instance. UserId. ToString (); if (Request. QueryString ["size"]! = Null) {size = Convert. ToInt32 (Request. QueryString ["size"]);} if (Request. QueryString ["path"]! = Null) {path = Request. queryString ["path"]. toString (). trim (). replace ('|', '/');} if (Request. queryString ["name"]! = Null) {name = Request. QueryString ["name"]. ToString (). Trim ();} if (Request. QueryString ["shape"]! = Null) {shape = Request. queryString ["shape"]. toString (). trim () ;}uploadmethod (files );}} /// <summary> /// upload an image /// </summary> /// <param name = "hc"> </param> public void uploadMethod (HttpFileCollection hc) {HttpPostedFile _ file = hc [0]; // file size long _ size = _ file. contentLength; if (_ size <= 0) {Response. write ("file error. "); Response. End (); return;} if (size * 1024 <_ size) {Response. Write (" the file is too large, and the maximum value is "+ size +" KB. "); Response. end (); return;} // file name string _ name = _ file. fileName; // File Format string _ tp = System. IO. path. getExtension (_ name ). toLower (); if (type. indexOf (_ tp) =-1) {Response. write ("File Format error. "); Response. end (); return;} // save path string _ path = HttpContext. current. server. mapPath (path) + @ "/" + name + _ tp; try {int w = Convert. toInt32 (shape. split ('*') [0]); int h = Convert. toInt32 (shape. split ('*') [1]); ImageHelper. cutForCustom (_ file, _ path, w, h, 50); Response. write (path + @ "/" + name + _ tp);} catch (Exception) {Response. write ("sorry, an error occurred. "); Response. End ();}}}}

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.