慕少森
Internet Explorer 10+// Define the stringvar string = 'Hello World!';// Encode the Stringvar encodedString = btoa(string);console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"// Decode the Stringvar decodedString = atob(encodedString);console.log(decodedString); // Outputs: "Hello World!"跨浏览器重写和模块化的UTF-8和Base 64 Javascript编码和解码库/模块的AMD,CommonJS,NodeJS和浏览器.跨浏览器兼容。使用Node.js下面是如何在Node.js中将普通文本编码为base 64://Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. // Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hexvar b = new Buffer('JavaScript');// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.// We can make it convert to other formats by passing the encoding type to toString().var s = b.toString('base64');下面是如何解码Base 64编码的字符串:var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')var s = b.toString();使用Dojo.js若要使用dojox.encoding.base 64编码字节数组,请执行以下操作:var str = dojox.encoding.base64.encode(myByteArray);若要解码base 64编码的字符串,请执行以下操作:var bytes = dojox.encoding.base64.decode(str)波尔安装角-底座64<script src="bower_components/angular-base64/angular-base64.js"></script>angular
.module('myApp', ['base64'])
.controller('myController', [
'$base64', '$scope',
function($base64, $scope) {
$scope.encoded = $base64.encode('a string');
$scope.decoded = $base64.decode('YSBzdHJpbmc=');
}]);但怎么做?如果您想了解更多关于base 64的一般编码方式,特别是JavaScript编码,我推荐本文:JavaScript中的计算机科学:base 64编码