Detailed form upload for Ajax file asynchronous upload

Source: Internet
Author: User
User uploads in the project is always indispensable, the following is the main list of the form upload and Ajax upload! Note: Context. Request.Files is not suitable for large files to operate, the following list is mainly for small file upload processing! This article is mainly for everyone to introduce the form upload function and file upload function realization, ajax file asynchronous upload, with a certain reference value, interested in small partners can refer to, hope to help everyone.

Resources Download:

First, jquery official download Address: https://jquery.com/download/

I. Form upload:

HTML Client section:


<form action= "Upload.ashx" method= "post" enctype= "Multipart/form-data" >    Select File: <input type= "file" Name= " File1 "/><br/>    <input type=" Submit "value=" Upload "/>  </form>

General handler server side:


public void ProcessRequest (HttpContext context)    {      context. Response.ContentType = "Text/plain";      Httppostedfile file1 = context. request.files["File1"];      Helper.uploadfile (File1, "~/upload/");//This is the call to the corresponding method      context. Response.Write ("OK");//Prompt execution successful    }

The encapsulation of the upload code:


<summary>///upload Pictures///</summary>//<param name= "file" > Express submitted files via form </param>//     <param name= "Virpath" > File to save virtual path </param> public static void Uploadimg (Httppostedfile file,string Virpath) {if (file.      ContentLength > 1024x768 * 4) {throw new Exception ("file cannot be greater than 4M"); } String imgtype = Path.getextension (file.      FileName);      if (imgtype!= ". jpg" &&imgtype!= ". jpeg")//Picture type restrictions {throw new Exception ("Please upload jpg or jpeg image"); } using (Image img = bitmap.fromstream (file. InputStream)) {string savepath = HttpContext.Current.Server.MapPath (virpath+file.        FileName); Img.      Save (Savepath); }}///<summary>///Upload files///</summary>//<param name= "file" > Express submitted files through form </param&    Gt <param name= "Virpath" > File to save virtual path </param> public static void UploadFile (Httppostedfile file, String virp ATH) {if (file. ContenTlength > 1024x768 * 6) {throw new Exception ("file cannot be greater than 6M"); } String imgtype = Path.getextension (file.      FileName); Imgtype restricts uploaded files if (Imgtype! = ". zip" && imgtype! = ". mp3") {throw new Exception ("Only allow upload of zip, R Ar....      Document ");      } string dirfullpath= HttpContext.Current.Server.MapPath (Virpath); if (!      Directory.Exists (Dirfullpath)//If the folder does not exist, first create the folder {Directory.CreateDirectory (Dirfullpath); } file. SaveAs (Dirfullpath + file.    FileName); }

Two. Asynchronous uploading of Ajax files:

Note: Since there is a form upload why do Ajax upload it? The entire page is refreshed because of the form upload process! Ajax asynchronous upload can be achieved only to refresh the local location, the following is a simple look at Ajax upload it!

HTML Client section:


General handler server side:


public void ProcessRequest (HttpContext context)  {   context. Response.ContentType = "text/html";   if (context. Request.Files.Count > 0)   {    Httppostedfile file1 = context. request.files["MyFile"];    Helper.uploadfile (File1, "~/upload/"); This refers to the method Writejson (context) that is encapsulated above    . Response, "true", "");   }   else   {    Writejson (context. Response, "error", "Please select File to upload");}  }

JSON Code encapsulation:


public static void Writejson (HttpResponse response,      string Status1, String Msg1, object data1 = null)    {      re Sponse. ContentType = "Application/json";      var obj = new {status = Status1, msg = MSG1, data = data1};      String json = new JavaScriptSerializer (). Serialize (obj);      Response. Write (JSON);    }

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.