Compatibility think good: Ff,ch,ie, Cheetah, can be achieved. If you see echo. Of course it's a success.
After several days of Non-stop, finally put the two goods out. It's a great pains. But the moment of doing it is quite happy.
The first step: we need to load a few JS libraries.
jquery library.
Jquery.form.js Library.
Download the two libraries and reference them to the page.
The following is the JS code in the page:
Copy Code code as follows:
function upload () {
var options = {
Type: "POST",//Of course this is the transmission mode
URL: '.. /include/files.ashx ',//General handler Path
Success:function (msg) {//returned parameters
$ ("#server_img"). attr ("src", msg); echo the picture.
}
};
Pass the options to Ajaxform
$ (' #aspnetForm '). Ajaxsubmit (options);
}
Step two: Code in the general handler
Copy Code code as follows:
public void ProcessRequest (HttpContext context)
{
httpfilecollection files = context. Request.Files; Get file objects from
if (Files. Count > 0)
{
String path = ""; Path string
Random rnd = new Random ();
for (int i = 0; i < files. Count; i++)
{
Httppostedfile file = Files[i]; Get File objects
if (file. ContentLength > 0)
{
String fileName = file. FileName;
string extension = Path.getextension (fileName);
int num = rnd. Next (5000, 10000); File name
Path = ".. /.. /userfiles/temp/"+ Num. ToString () + extension;
File. SaveAs (System.Web.HttpContext.Current.Server.MapPath (path)); Save the file.
}
}
Context. Response.Write (path); Returns the path after the file is stored for Echo.
}
}
Step three: Code in HTML or ASPX.
The following two lines of code are arbitrarily inserted anywhere in the HTML or ASPX. Come to think of it can be achieved.
Copy Code code as follows:
//For echoing pictures
<asp:fileupload id= "Up_load" runat= "onchange=" Upload () "ontextchange=" Upload () "/>//upload pictures, Automatic, two events to Ensure all browsers are compatible.