Asp.net + jquery. form asynchronous Image Upload method (attached to jquery. form. js download ),
This document describes how to use asp.net + jquery. form to upload images asynchronously. We will share this with you for your reference. The details are as follows:
First, we need to prepare:
Click here to download jquery.
Click here to download jquery. form. js.
Page JqueryFormTest. aspx:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "JqueryFormTest. aspx. cs" Inherits = "JqueryFormTest" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Img. ashx:
<% @ WebHandler Language = "C #" Class = "img" %> using System; using System. web; public class img: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; // get the object of the uploaded file HttpPostedFile img = context. request. files ["btnfile"]; // get the name of the uploaded file string s = img. fileName; // obtain the name of the uploaded file. (the absolute path is also associated during ie upload. Only the file name is obtained here.) string str = s. substring (s. lastIndexOf ("\") + 1); s Tring path = "~ /Upload/"+ str; // Save the img file. saveAs (context. server. mapPath (path); // HttpRuntime. appDomainAppVirtualPath is used to obtain the virtual path name of the application. Because the image context cannot be displayed because it is not automatically added when the response is sent to the page. response. write (HttpRuntime. appDomainAppVirtualPath + path. substring (1); // path. substring (1) is used to remove the first ~ Character} public bool IsReusable {get {return false ;}}}