jquery No refresh upload learning experience

Source: Internet
Author: User

Remember just left the university, into the current company less than one months, there is a senior to us at the time of the new people told the next JS no refresh upload related knowledge.

Previously, the server upload control FileUploadwas used in C #, but each time it was used, the page was refreshed and the feeling was not too good. But that is, not too concerned about this detail, and JS knowledge is very scarce, so did not find a solution.

At the time, one of the methods mentioned by the predecessor was:

Prepare a homepage (user interface) and an upload page (in the main page hidden IFrame, as a really meaningful upload page), when you click on the homepage of the upload button, the upload page is actually called the upload control, so as to achieve the upload. And this set of operation is not no refresh, but the refresh is done on the upload page, the user does not feel it.

This principle is actually, a iframe+ a form+ an upload control, upload is actually submitted under the IFrame form, the above method, the need to use the IFRAME, form and upload controls are written in advance, there is a real page.

We can create IFRAME, form, and upload controls dynamically using JS or jquery, as previously understood online.

The steps are as follows:

1. Create an iframe to add to the document

1 var iframe = $ ("<iframe src= ' javascript:false; ' id= '" + ID + "' name= '" + ID + "' border= ' none ' width= ' 0 ' Heig ht= ' 0 ' ></iframe> "). AppendTo (Document.body) [0];
$ (IFRAME). attr ("onload", "Iframeload ('" + ID + "', '" + This._imgpanel + "')");

2. Create a form, add it to the document, and point its target property to the IFRAME created above

1 var this. _url + "' method= ' POST ' enctype= ' multipart/form-data ' ></form>"). AppendTo (document.body) [0 ]; 2         Form.target = Iframe.name;

3. Create an upload control and add it to the form above

1         this. _control = $ ("<input type= ' file ' id= '" + ID + "' name= '" + ID + "' style= ' Filter:alpha (opacity=100); '/ > "); 2          This . _control.appendto (form); 3         this. _control.attr ("onchange", "Filechangehandler (This, '" + form.id + "', '" + ID + "')");

4, the implementation of this upload control onchange event, has completed the upload

1 var function (obj, Formid, fupid) {2     // /<summary> automatically upload </summary> when you finish selecting a file 3     $ ("#" + Formid). Submit (); 4 };

5, after the submission needs to receive the background and upload. Since this is a C # development, here is a C # upload code (good at other programming languages, can be used in their own way). First build a generic handler handler.ashx, then get the submitted file, finally implement the upload and return important information.

1Request =context. Request;2         //gets the collection of files uploaded to the server3httpfilecollection files = request.files;//The maxRequestLength property to configure the HttpRuntime node is a large value, otherwise an oversized file cannot be uploaded and an exception is thrown4String Newfilepath = "";5         if(Files. Count > 0)6         {7String path = Server.MapPath ("upfiles/");8             if(!directory.exists (path))9             {Ten directory.createdirectory (path); One             } AHttppostedfile file = files[0]; -String newfilename = DateTime.Now.ToString ("YYYYMMDDHHMMDD") +path.getextension (file. FileName); -File. SaveAs (path +newfilename); theNewfilepath = "upfiles/" +NewFileName; -         }
- - Response.Write (Newfilepath); +Response.End ();

6, the foreground receives the return message the method as follows

1 var msg = $ ("#" + Iframeid) [0].contentdocument.body.innerhtml;

Note: Iframeid is the ID of the IFRAME created. The message returned in the background will be displayed in the body of the IFRAME, where it is only necessary to fetch this value.

Specific implementation I put in a demo, interested in the park friends can download down to read, if there are shortcomings, but also hope to criticize treatise.

Thank!

Http://files.cnblogs.com/files/jijiexuanfeng/JsNoRefresh.rar

jquery No refresh upload learning experience

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.