简体   繁体   English

浏览器和nodejs上有没有可以将base64字符串转换为utf8字符串的API?

[英]Is there any API on the browser and nodejs that can convert base64 string to utf8 string?

const atob = (encodedString) => Buffer.from(encodedString, 'base64').toString('latin1')

Is there an API that can be used normally in nodejs and browsers that can help me convert base64 to utf8 string?有没有可以在nodejs和浏览器中正常使用的API可以帮我把base64转utf8字符串?

In node.js, we can write:在 node.js 中,我们可以这样写:

const base64ToUtf8 = (encodedString) => Buffer.from(encodedString, 'base64').toString('utf8')

I need to use this function to process base64 encoded utf8 data-uri in node.js and browser.我需要使用这个函数在 node.js 和浏览器中处理 base64 编码的 utf8 data-uri。

This node-js code reads the data encoded in this base64 string "as UTF-8" .此 node-js 代码读取此 base64 字符串中编码的数据"as UTF-8" Beware it's not the same as converting "to an UTF-8 string", since JavaScript only supports UCS-2 and UTF-16 strings.请注意,这与转换“为 UTF-8 字符串”不同,因为 JavaScript 仅支持 UCS-2 和 UTF-16 字符串。

This nitpick passed, there are many methods to do the same as this code in a browser.这个挑剔通过了,有很多方法可以在浏览器中完成与此代码相同的操作。

The cleanest is to first decode this base64 string in an ArrayBuffer and then read that ArrayBuffer as string, just like your node code:最干净的方法是首先在 ArrayBuffer 中解码这个 base64 字符串,然后将该 ArrayBuffer 作为字符串读取,就像您的节点代码一样:

 const b64 = "wqFIb2xhIG5pw7FvcyE="; const buf = base64ToBuf(b64); const txt = new TextDecoder().decode(buf); console.log(txt); function base64ToBuf(str) { const binStr = atob(str); const len = binStr.length; const arr = new Uint8Array(len); for (let i = 0; i < len; i++) { arr[i] = binStr.charCodeAt(i); } return arr; }

An other, shorter in code, but implying more IO is to fetch it as text:另一个代码较短但暗示更多 IO 的方法是将其作为文本获取

 const b64 = "wqFIb2xhIG5pw7FvcyE="; fetch('data:text;base64,' + b64) .then(r => r.text()) .then(txt => console.log(txt));

And you could also make use of the FileReader API's readAsText() , which might be useful in case you want to support more encodings than utf-8.您还可以使用 FileReader API 的readAsText() ,如果您想支持比 utf-8 更多的编码,这可能很有用。

 const b64 = "wqFIb2xhIG5pw7FvcyE="; const blob = base64ToBlob(b64); const reader = new FileReader(); reader.onload = e => { const txt = reader.result; console.log(txt); }; reader.readAsText(blob, "utf8"); function base64ToBlob(str) { const binStr = atob(str); const len = binStr.length; const arr = new Uint8Array(len); for (let i = 0; i < len; i++) { arr[i] = binStr.charCodeAt(i); } return new Blob([arr]); }

There is also the new Blob.text() that does convert to utf-8 directly, but from a base64 string, there is very little interest in going that road.还有新的Blob.text()可以直接转换为 utf-8,但是从 base64 字符串来看,走这条路的兴趣很小。 Still for the curious:仍然是好奇的:

 const b64 = "wqFIb2xhIG5pw7FvcyE="; const blob = base64ToBlob(b64); blob.text().then(txt => console.log(txt)); function base64ToBlob(str) { const binStr = atob(str); const len = binStr.length; const arr = new Uint8Array(len); for (let i = 0; i < len; i++) { arr[i] = binStr.charCodeAt(i); } return new Blob([arr]); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM