Asp.net 2.0 no-refreshing Image Upload + echo

Source: Internet
Author: User

Simple three-step Image Upload without refreshing: Pay attention to the upload. For verification during the upload, the size, size, and format of the image are different from those of the image. Solve the problem by yourself. If I do, it will also be pasted.

Good compatibility: FF, CH, IE, And cheetah can all be implemented. If the ECHO is displayed. Of course it is successful.

After several days of continuous exploitation, I finally got the second goods out. It's really painstaking. However, the moments are quite fun.

Step 1: We need to load several JS libraries.
Jquery library.
Jquery. form. js library.

Download the two libraries and reference them to the page.

The following is the JS Code on the page:
Function upload (){
Var options = {
Type: "POST", // Of course, this is the transfer method
Url: '../Include/Files. ashx', // path of the general Handler
Success: function (msg) {// Response Parameter
$ ("# Server_img"). attr ("src", msg); // echo the image.
}
};
// Pass options to ajaxForm
$ ('# AspnetForm'). ajaxSubmit (options );
}

Step 2: generally process the code in the program

Public void ProcessRequest (HttpContext context)
{
HttpFileCollection files = context. Request. Files; // obtain the object 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 the file object
If (file. ContentLength> 0)
{
String fileName = file. FileName;
String extension = Path. GetExtension (fileName );
Int num = rnd. Next (5000,100 00); // file name
Path = "../UserFiles/temp/" + num. ToString () + extension;
File. SaveAs (System. Web. HttpContext. Current. Server. MapPath (path); // save the file.
}
}
Context. Response. Write (path); // return the path of the stored file for ECHO.
}
}

Step 3: code in html or aspx.
The following code inserts any location in html or aspx. All can be implemented.

// used to display an image
<Asp: FileUpload ID = "Up_load" runat = "server" onchange = "upload ()" ontextchange = "upload ()"/> // upload an image, automatic, the two events are designed to ensure compatibility with all browsers.

Related Article

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.