用Javascript進行base64編碼,在高版本的IE瀏覽器(IE9以上版本),和firefox,chrome瀏覽器裏是很是方便的。這些瀏覽器的window對象中內置了base64的編碼和解碼方法。javascript
var base64String = window.btoa(string) ;//編碼html
var string = window.atob(base64string) ;//解碼java
注意:這對編碼解碼方法只是支持ascii字符集編碼,不支持unicode字符集。也就是若是給window.btoa方法的參數是漢字的話,該方法就會報錯。因此建議:若是參數多是unicode字符的話,贊成用下面的自定義的base64編碼辦法會好一點兒。chrome
既然瀏覽器自帶的base64編碼不支持unicode字符集的字符,那麼咱們就想辦法讓他支持。辦法就是先把unicode字符集編碼,編碼的結果就是隻含有utf-8碼。看下面的方法。至於爲何要使用encodeURIComponent,escape,請看這裏瀏覽器
1 function utf8_to_b64(utf8){ 2 return window.btoa(encodeURIComponent(escape(utf8))); 3 } 4 function b64_to_utf8(b64){ 5 return unescape(decodeURIComponent(window.atob(b64))); 6 }
不幸的是,低版本的ie瀏覽器裏沒有這麼一對編碼/解碼的方法。怎麼辦?本身提供,看下面的代碼:編碼
1 (function(){ 2 if(window.atob) 3 return; 4 else{ 5 var Base64 = window.Base64 || { 6 /* private property*/ 7 _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 8 9 /* public method for encoding */ 10 encode : function (input) { 11 var output = ""; 12 var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 13 var i = 0; 14 15 input = Base64._utf8_encode(input); 16 17 while (i < input.length) { 18 19 chr1 = input.charCodeAt(i++); 20 chr2 = input.charCodeAt(i++); 21 chr3 = input.charCodeAt(i++); 22 23 enc1 = chr1 >> 2; 24 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 25 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 26 enc4 = chr3 & 63; 27 28 if (isNaN(chr2)) { 29 enc3 = enc4 = 64; 30 } else if (isNaN(chr3)) { 31 enc4 = 64; 32 } 33 34 output = output + 35 Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) + 36 Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4); 37 38 } 39 40 return output; 41 }, 42 43 /* public method for decoding */ 44 decode : function (input) { 45 var output = ""; 46 var chr1, chr2, chr3; 47 var enc1, enc2, enc3, enc4; 48 var i = 0; 49 50 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 51 52 while (i < input.length) { 53 54 enc1 = Base64._keyStr.indexOf(input.charAt(i++)); 55 enc2 = Base64._keyStr.indexOf(input.charAt(i++)); 56 enc3 = Base64._keyStr.indexOf(input.charAt(i++)); 57 enc4 = Base64._keyStr.indexOf(input.charAt(i++)); 58 59 chr1 = (enc1 << 2) | (enc2 >> 4); 60 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 61 chr3 = ((enc3 & 3) << 6) | enc4; 62 63 output = output + String.fromCharCode(chr1); 64 65 if (enc3 != 64) { 66 output = output + String.fromCharCode(chr2); 67 } 68 if (enc4 != 64) { 69 output = output + String.fromCharCode(chr3); 70 } 71 72 } 73 74 output = Base64._utf8_decode(output); 75 76 return output; 77 78 }, 79 80 /* private method for UTF-8 encoding */ 81 _utf8_encode : function (string) { 82 string = string.replace(/\r\n/g,"\n"); 83 var utftext = ""; 84 85 for (var n = 0; n < string.length; n++) { 86 87 var c = string.charCodeAt(n); 88 89 if (c < 128) { 90 utftext += String.fromCharCode(c); 91 } 92 else if((c > 127) && (c < 2048)) { 93 utftext += String.fromCharCode((c >> 6) | 192); 94 utftext += String.fromCharCode((c & 63) | 128); 95 } 96 else { 97 utftext += String.fromCharCode((c >> 12) | 224); 98 utftext += String.fromCharCode(((c >> 6) & 63) | 128); 99 utftext += String.fromCharCode((c & 63) | 128); 100 } 101 102 } 103 104 return utftext; 105 }, 106 107 /* private method for UTF-8 decoding */ 108 _utf8_decode : function (utftext) { 109 var string = ""; 110 var i = 0; 111 var c = c1 = c2 = 0; 112 113 while ( i < utftext.length ) { 114 115 c = utftext.charCodeAt(i); 116 117 if (c < 128) { 118 string += String.fromCharCode(c); 119 i++; 120 } 121 else if((c > 191) && (c < 224)) { 122 c2 = utftext.charCodeAt(i+1); 123 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 124 i += 2; 125 } 126 else { 127 c2 = utftext.charCodeAt(i+1); 128 c3 = utftext.charCodeAt(i+2); 129 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 130 i += 3; 131 } 132 133 } 134 return string; 135 } 136 } 137 window.Base64 = Base64; 138 } 139 })();
這樣,就能夠爲一些瀏覽器裏不支持base64編碼瀏覽器提供一種方法。base64編碼的代碼摘自這裏。url