为了账号安全,请及时绑定邮箱和手机立即绑定

Base 64在客户端Javascript中的编码和解码

Base 64在客户端Javascript中的编码和解码

喵喔喔 2019-06-25 17:31:51
Base 64在客户端Javascript中的编码和解码JavaScript中是否有任何方法可用于使用base 64编码对字符串进行编码和解码?
查看完整描述

3 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

在基于gecko/webkit的浏览器(Firefox、Chrome和Safari)和Opera中,您可以使用btoa()阿托布().

原文:如何用JavaScript将字符串编码为base 64?


查看完整回答
反对 回复 2019-06-25
?
慕少森

TA贡献2019条经验 获得超9个赞

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编码


查看完整回答
反对 回复 2019-06-25
  • 3 回答
  • 0 关注
  • 293 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信