jquery Plugin---Simulate from form upload file for asynchronous commit

Source: Internet
Author: User

Jquery.extend ({createuploadiframe:function (id, URI) {//create frame var Frameid = ' Juploadframe ' + ID        ; var iframehtml = ' <iframe id= ' + Frameid + ' "name=" ' + Frameid + ' "style=" position:absolute; top:-9999px;        Left:-9999px "'; if (window. ActiveXObject) {if (typeof uri = = ' Boolean ') {iframehtml + = ' src= "' + ' javascript:false ' + ')            ‘;            } else if (typeof uri = = ' string ') {iframehtml + = ' src= "' + uri + '";        }} iframehtml + = '/> ';        JQuery (iframehtml). AppendTo (Document.body);    return JQuery (' # ' + Frameid). Get (0); }, Createuploadform:function (ID, Fileelementid, data, fileElementId2) {//create form var formId = ' JUp        Loadform ' + ID;        var fileId = ' juploadfile ' + ID; var form = jQuery (' <form action= "" method= "POST" name= "' + formId + '" id= "' + formId + '" enctype= "Multipart/form-dat        A "></form>");if (data) {for (var i in data) {jQuery (' <input type= "hidden" name= "' + i + '" value= "' + da            Ta[i] + '/> '). AppendTo (form);        }} var oldelement = JQuery (' # ' + Fileelementid);        var newelement = jQuery (oldelement). Clone ();        JQuery (oldelement). attr (' id ', fileId);        JQuery (Oldelement). before (newelement);        JQuery (oldelement). AppendTo (form);            if (fileElementId2) {var oldElement2 = jQuery (' # ' + fileElementId2);            var newElement2 = jQuery (OldElement2). Clone ();            JQuery (OldElement2). attr (' id ', fileId);            JQuery (OldElement2). before (newelement);        JQuery (OldElement2). AppendTo (form);        }//set attributes JQuery (form). CSS (' position ', ' absolute ');        JQuery (Form). CSS (' top ', ' -1200px ');        JQuery (Form). CSS (' left ', ' -1200px ');        JQuery (Form). AppendTo (' body ');    return form; }, Ajaxfileupload:function (s) {//TODO introDuce global settings, allowing the client to modify them for all requests, not only timeout S = jquery.extend ({}, J        Query.ajaxsettings, s);        var id = new Date (). GetTime ();        var form;  if (S.morethan) {form = Jquery.createuploadform (ID, S.fileelementid, (typeof (S.data) = = ' undefined '? False:        S.data), S.FILEELEMENTID2); } else {form = Jquery.createuploadform (ID, S.fileelementid, (typeof (S.data) = = ' undefined '? false:s        . data));        } var io = jquery.createuploadiframe (ID, S.secureuri);        var Frameid = ' juploadframe ' + ID;        var formId = ' juploadform ' + ID; Watch for a new set of requests if (S.global &&!jquery.active++) {JQuery.event.trigger ("Aja        Xstart ");        } var requestdone = false; Create the Request object var xml = {} if (S.global) JQuery.event.trigger ("Ajaxsend", [XML, S]        ); Wait for a response to come BACK var uploadcallback = function (istimeout) {var io = document.getElementById (Frameid); try {if (Io.contentwindow) {xml.responsetext = Io.contentWindow.document.body? io.con                    TentWindow.document.body.innerHTML:null; Xml.responsexml = io.contentWindow.document.XMLDocument?                Io.contentWindow.document.XMLDocument:io.contentWindow.document; } else if (io.contentdocument) {xml.responsetext = Io.contentDocument.document.body? io.contentdocume                    Nt.document.body.innerHTML:null; Xml.responsexml = io.contentDocument.document.XMLDocument?                Io.contentDocument.document.XMLDocument:io.contentDocument.document;            }} catch (e) {Jquery.handleerror (s, XML, NULL, E);                } if (XML | | istimeout = = "Timeout") {Requestdone = true;                var status;             try {       Status = istimeout! = "Timeout"?                    "Success": "Error";                        Make sure, the request was successful or notmodified if (Status! = "Error") { Process the data (runs the XML through Httpdata regardless of callback) var data = jquery.u                        Ploadhttpdata (XML, S.datatype);                            If a local callback is specified, fire it and pass it the data if (s.success)                        S.success (data, status); Fire the global callback if (S.global) JQuery.event.trigger ("Ajaxsucc                    ESS ", [XML, S]);                } else Jquery.handleerror (S, XML, status);                    } catch (e) {status = "error";                Jquery.handleerror (S, XML, status, E);   }//The request was completed if (S.global)                 JQuery.event.trigger ("Ajaxcomplete", [XML, S]); Handle the Global AJAX counter if (S.global &&!--jquery.active) Jquery.even                T.trigger ("Ajaxstop");                Process result if (s.complete) s.complete (XML, status); jquery (IO). Unbind () setTimeout (function () {try {jQuery (IO). rem                        Ove ();                    JQuery (Form). Remove ();                    } catch (E) {Jquery.handleerror (s, XML, NULL, E); }}, +) XML = null}}//Timeout checker if (s.timeout &G T 0) {setTimeout (function () {//Check to see if the request is still happening I            F (!requestdone) Uploadcallback ("timeout");        }, S.timeout); } try {var form = JqueRy (' # ' + formId);            JQuery (Form). attr (' action ', S.url);            JQuery (Form). attr (' method ', ' POST ');            JQuery (Form). attr (' target ', Frameid);            if (form.encoding) {jQuery (form). attr (' Encoding ', ' multipart/form-data ');            } else {jQuery (form). attr (' enctype ', ' multipart/form-data ');        } jQuery (Form). Submit ();        } catch (E) {Jquery.handleerror (s, XML, NULL, E);        } jQuery (' # ' + Frameid). Load (uploadcallback);    return {abort:function () {}};        }, Uploadhttpdata:function (r, type) {var data =!type; data = Type = = "xml" | | Data?        R.responsexml:r.responsetext;        If the type is ' script ', eval it in global context if (type = = "Script") jquery.globaleval (data);        Get the JavaScript object, if JSON is used.        if (type = = "json") eval ("data =" + data); Evaluate scripts within HTML if (type = = "html") jQuery ("<div>"). HTML (data). Evalscripts ();    return data; }})

Using:

            $.ajaxfileupload ({                URL: ',                secureuri:false,                data: {                    LoginName: _loginname,//user name                    MPSW: _mpsw,/ /password                    REPSW: _repsw,//Confirm password                    mtel: _mtel,//phone                    mname: _mname,//real name                    vcode: _vcode,//Authentication Code                    CName: _ cname,//Company Name                    LName: _lname,//legal person                    lpid: _lpid,//ID number                    caddress: _caddress,//company Address                },                Fileelementid: ' file1 ',//upload file Control                fileElementId2: ' file2 ',//upload file control                morethan:true,//support two image uploads                DataType: ' json ',//return value type                success:function (data, status) {                    alert (' Handle code! }                ,                error:function (data, status, E) {                    alert (' An error occurred ');}            });

  

JQuery Plugin---Simulate from form upload file for asynchronous commit

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.