var Dom ={}; Dom. base64encode = function (data) {var xml_dom = new activexobject ("Microsoft. xmldom "); xml_dom.loadxml ('
'); xml_dom.documentelement.setattribute ("xmlns: DT", "urn: Schemas-Microsoft-com: datatypes "); vaR node1 = xml_dom.createelement ("file1"); node1.datatype = "bin. base64 "; node1.nodetypedvalue = data; xml_dom.documentelement.appendchild (node1); Return node1.text;} var FSO = new activexobject ('scripting. fileSystemObject '); var stream = new activexobject ('ADODB. stream '); stream. type = 1; // 1 = binary, 2 = text stream. open (); stream. loadfromfile (FSO. getabsolutepathname ("/images/rails.png"); var bin = stream. read (-1); //-1: Read all,-2: Read line var ret = Dom. base64encode (BIN);
AboveCodeYou need to minimize the security level of the browser, and other settings may be required. However, I later found a better way to read data streams. After all, scripting. FileSystemObject is too unreliable and we need a more common Host object. IE's Ajax implementation is undoubtedly the best choice.
VaR HTTP = new activexobject ("Microsoft. XMLHTTP "); http. open ("get", "images/default/logo.gif", false); http. send (); var xml = document. createelement ("XML"); XML. loadxml ("
"); Var root = xml.doc umentelement; root. datatype =" bin. base64 "; root. nodetypedvalue = http. responsebody; alert (root. Text );
In the cutting-edge browser that supports HTML5, they have an API called todataurl to implement this binary conversion to base64 function.
String Conversion encoding is much simpler. Firefox has two methods, atob and btoa. For details, see here.
Finally, the compatibility Solution of a Japanese JS master is attached:
/** $ ID: base64.js, v 1.1 2009/03/01 22:38:45 dankogai exp dankogai $ ** history: * dankogai's original: character-based * drry's fix: split string to array then join * New Version: Regexp-based */(function () {var b64chars = 'callback +/'; var b64tab = function (BIN) {var T = {}; for (VAR I = 0, L = bin. length; I> 18) + b64chars. charat (n >>> 12) & 63) + b64chars. charat (n >>> 6) & 63) + b64chars. charat (N & 63) ;}; var tobase64 = function (BIN) {If (bin. match (/[^ \ x00-\ xFF]/) Throw 'unsupported orted character found'; var padlen = 0; while (bin. length % 3) {bin + = '\ 0'; padlen ++;}; var b64 = bin. replace (/[\ x00-\ xFF] {3}/g, sub_tobase64); If (! Padlen) return b64; b64 = b64.substr (0, b64.length-padlen); While (padlen --) b64 + = '; return b64 ;}; var btoa = Window. btoa | tobase64; var sub_frombase64 = function (m) {var n = (b64tab [M. charat (0)]> 16) + String. fromcharcode (n> 8) & 0xff) + String. fromcharcode (N & 0xff) ;}; var frombase64 = function (b64) {b64 = b64.replace (/[^ A-Za-z0-9 \ + \/]/g ,''); vaR padlen = 0; while (b64.length % 4) {b64 + = 'a'; padlen ++ ;} vaR bin = b64.replace (/[A-Za-z0-9 \ + \/] {4}/g, sub_frombase64); bin. length-= [,] [padlen]; return bin ;}; var atob = Window. atob | frombase64; var re_char_nonascii =/[^ \ x00-\ xFF]/g; var sub_char_nonascii = function (m) {var n = m. charcodeat (0); Return n> 6) + String. fromcharcode (0x80 | (N & 0x3f): string. fromcharcode (0xe0 | (n >>> 12) & 0x0f) + String. fromcharcode (0x80 | (n >>> 6) & 0x3f) + String. fromcharcode (0x80 | (N & 0x3f) ;}; var utob = function (UNI) {return uni. replace (re_char_nonascii, sub_char_nonascii );}; vaR re_bytes_nonascii =/[\ xc0-\ xdf] [\ X80-\ xbf] | [\ xe0-\ XeF] [\ X80-\ xbf] {2} | [\ xf0 -\ xf7] [\ X80-\ xbf] {3}/g; vaR sub_bytes_nonascii = function (m) {var C0 = m. charcodeat (0); var C1 = m. charcodeat (1); If (C0