客户端 Javascript 中的 Base64 编码和解码
JavaScript 中是否有任何方法可用于使用 base64 编码对字符串进行编码和解码?
Are there any methods in JavaScript that could be used to encode and decode a string using base64 encoding?
推荐答案
一些浏览器,如 Firefox、Chrome、Safari、Opera 和 IE10+ 可以原生处理 Base64.看看这个 Stackoverflow 问题.它使用 btoa()
和 atob()
函数.
Some browsers such as Firefox, Chrome, Safari, Opera and IE10+ can handle Base64 natively. Take a look at this Stackoverflow question. It's using btoa()
and atob()
functions.
对于服务器端的JavaScript(Node),可以使用Buffer
s进行解码.
For server-side JavaScript (Node), you can use Buffer
s to decode.
如果您要使用跨浏览器解决方案,可以使用现有的库,例如 CryptoJS 或类似代码:
If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:
http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html(存档)
对于后者,您需要彻底测试该功能的跨浏览器兼容性.并且错误已被报告.
With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.
相关文章