ASP. NET 2.0 no refresh image upload display thumbnail specific implementation

Source: Internet
Author: User
Tags httpcontext jquery library

Simple three-step implementation of the picture without refreshing upload: Note is uploaded, as for the verification of the upload, compared to the size of the film, size, format judgment, restrictions, etc., self-solve.

Compatibility wants to be good: Ff,ch,ie, cheetah, all can be realized. If you see echo. Of course it was a success.

After a few days of constant in the end, finally the two goods out. It was a great painstaking. But the moment of making out is quite happy.

The first step: 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 delivery method        URL: '. /include/files.ashx ',        //General handler path        success:function (msg) {        ////Return parameter            $ ("#server_img"). attr ("src", msg);            echo the picture.        }     };     Pass the options to Ajaxform     $ (' #aspnetForm '). Ajaxsubmit (options);}

Step two: code within the general handler

public void ProcessRequest (HttpContext context) {httpfilecollection files = context.              Request.Files; Gets the file object if (files.                                                            Count > 0) {String path = "";            Path string Random rnd = new Random (); for (int i = 0; i < files. Count;                                        i++) {Httppostedfile file = Files[i]; Gets the file object if (files. 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 literally inserted anywhere in the HTML or ASPX. It's all possible to think.

   //For echoing picture <asp:fileupload id= "up_load" runat= "server" onchange= "Upload ()"  Ontextchange= "Upload ()"/>  //upload pictures, automatic, two events are to ensure that all browsers are compatible.

  

ASP. NET 2.0 no refresh image upload display thumbnail specific implementation

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.