Ajax file upload successfully resolves browser compatibility issues _ajax related

Source: Internet
Author: User
Tags extend new set

Ajaxfileupload control is very useful, but found that the results of uploading files for browser processing, IE will not, Google and Firefox will be processed, and Google and Firefox after the results are not the same, here for the above three browser compatibility adjustment.

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,fileelement) {//create form var formid = ' Juploadform '
    + ID;
    var Fileid = ' juploadfile ' + ID; var form = jQuery (' <form action= "" method= "POST" name= "' + Formid + '" id= "' + Formid + '" enctype= "multipart/form-data  
    "></form>"); if (data) {for (var i in data) {JQuery (' &Lt;input type= "hidden" name= "' + i + '" "value=" ' + data[i] + ' "/> '). Appendto (form);
    } var oldelement;
    if (fileelement = = null) Oldelement = JQuery (' # ' + Fileelementid);
     
    else oldelement = fileelement;
    var newelement = jQuery (oldelement). Clone ();
    JQuery (oldelement). attr (' id ', fileid);
    JQuery (Oldelement). before (newelement);
     
    JQuery (oldelement). 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 req
    Uests, not only timeout S = jquery.extend ({}, jquery.ajaxsettings, s); var id = new Date (). GetTime () var form = Jquery.createuploadform (ID, S.fileelementid, (typeof (S.data) = ' undefined '
    ? false:s.data), s.fileelement); 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.gete
      Lementbyid (Frameid); try {if (Io.contentwindow) {xml.responsetext = Io.contentwindow.document.body?io
           . contentWindow.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.contentdocument.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 this request was successful or notmodified if (status!= "error") {//
            Process the data (runs the XML through Httpdata regardless of callback) Console.log (Jquery.uploadhttpdata);  
            var data = Jquery.uploadhttpdata (Xml,s.datatype); If a local callback is specified, fire it and pass it the data if (s.success) s.success (d
   
            ATA, status); Fire the global callback if(S.global) JQuery.event.trigger ("Ajaxsuccess", [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.event.trigger ("AJ
 
        Axstop ");
 
        Process result if (s.complete) s.complete (XML, status); JQuery (IO). Unbind () settimeout (function () {try {j
                      Query (IO). Remove (); 
                       
                    JQuery (Form). Remove ();
                    catch (E) {Jquery.handleerror (s, XML, NULL, E);                  
 
           }       (MB) XML = null}//Timeout checker if (S.timeout > 0) {settim Eout (function () {//Check to = If the request is still happening if (!requestdone) Uploadcallback ("Tim
      Eout ");
    }, S.timeout);
      The 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 () {try {jQuery (' # ' + Frameid). Remove ();
      JQuery (Form). Remove ();
  The catch (e) {}}};
 
  },Uploadhttpdata:function (r, type) {var data = "";
    Data = (Type = = "xml"? r.responsexml:r.responsetext);
    if (type = = "Script") jquery.globaleval (data); 
      /** * Auth Garen 2016-06-17 * The response results after file upload, support IE FF GC * * * */if (type = = "json") {var reg = "";
      if (Data.indexof ("<pre>") >-1) {reg=/<pre> (. +) <\/pre>/g;  
      }else{reg=/<pre.+?> (. +) <\/pre>/g;
      var result = Data.match (reg); 
       var stri1=regexp.$1;     
       if (stri1!=null&&stri1!= "" &&stri1.trim (). length>0) {data = Stri1;
    eval ("data =" + data);
    } if (type = = "html") jQuery ("<div>"). HTML (data). Evalscripts ();
  return data; 
      }, Handleerror:function (S, XML, status, E) {//If a local callback is specified, fire it If (s.error)
 
    S.error (XML, status, E);
   Fire the global callback if (S.global)   JQuery.event.trigger ("Ajaxerror", [XML, S, E]); }
});

The above is the entire content of this article, I hope to help you learn.

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.