Application of AjaxFileUpload in C,

Source: Internet
Author: User

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.

 

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.