asp.net 2.0 no refresh picture upload display thumbnail specific implementation _ practical skills

Source: Internet
Author: User

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.

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.