Application of AjaxFileUpload in C,
I. Front-end page
<! DOCTYPE html>
Ii. upload function code upload. ashx
Using System; using System. IO; using System. web; using System. web. script. serialization; public class upload: IHttpHandler {public void ProcessRequest (HttpContext context) {// the file size is limited to 10 M const int maxSize = 10485760; // define the file extension var ext = new [] {"rar", "zip", "gif", "jpg", "jpeg", "png ", "bmp", "xls", "xlsx", "doc", "docx", "et", "wps"}; string strExt = ""; foreach (var a in ext) {strExt + = a + "/";} StrExt = strExt. trimEnd ('/'); string savePath = "/upLoads/"; // file storage path var resp = new UploadResponse (); HttpFileCollection imgFile = context. request. files; if (imgFile. count> 0) {string fileExt = Path. getExtension (imgFile [0]. fileName); // obtain the extension if (string. isNullOrEmpty (fileExt) | Array. indexOf (ext, fileExt. substring (1 ). toLower () <0) {resp. eor = "error"; resp. msg = string. format ("the extension is {0} Files cannot be uploaded! \ N only supports uploading files in {1} format. ", FileExt, strExt);} else {if (imgFile [0]. inputStream. length> maxSize) {resp. eor = "error"; resp. msg = "the size of the uploaded file exceeds the limit! ";} Else {string fileNewName = DateTime. now. toString ("yyyyMMddHHmmssffff") + fileExt; // new file name try {SaveFile (imgFile [0], context. server. mapPath (savePath), fileNewName); // save the file resp. eor = "succss"; resp. msg = "Upload successful! File Size: "+ imgFile [0]. contentLength; resp. imgUrl = savePath + fileNewName; resp. FName = fileNewName; resp. OName = imgFile [0]. fileName;} catch (Exception ex) {resp. eor = "error"; resp. msg = ex. toString () ;}}} else {resp. eor = "error"; resp. msg = "select a file! ";} Context. response. contentType = "text/html"; context. response. write (new JavaScriptSerializer (). serialize (resp); context. response. end ();} private void SaveFile (HttpPostedFile imgFile, string savePath, string fileName) {if (! Directory. exists (savePath) // determines whether the file storage path Exists {Directory. createDirectory (savePath);} imgFile. saveAs (Path. combine (savePath, fileName);} public class UploadResponse {public string Eor {get; set;} public string Msg {get; set;} public string ImgUrl {get; set ;} public string FName {get; set;} public string OName {get; set ;}} public bool IsReusable {get {return false ;}}}
Iii. Adjustment of returned json data
4. Source Code: http://pan.baidu.com/s/1jGo1wzW
We warmly welcome you to speak out.