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); }