简体   繁体   English

将字符串转换为 JavaScript 中的 base64。 btoa 和 atob 已弃用

[英]Convert a string to base64 in JavaScript. btoa and atob are deprecated

I have been working on some projects in VS Code lately and suddenly started receiving notifications in my code that btoa and atob are now deprecated.我最近一直在 VS Code 中处理一些项目,突然开始在我的代码中收到通知,说 btoa 和 atob 现在已弃用。 I can't find any resource for this besides VS Code.除了 VS Code,我找不到任何资源。 If this is true, what alternative is there?如果这是真的,还有什么选择?

btoa and atob are only deprecated for Node JS. btoaatob仅在 Node JS 中被弃用。 If you prepend the window.如果您预先window. you will get rid of this deprecation mark.您将摆脱此弃用标记。

On the other hand, if you are trying to use btoa o atob in the back-end side, you definitely should use Buffer interface.另一方面,如果您尝试在后端使用 btoa o atob,则绝对应该使用Buffer接口。

References:参考:

  1. https://github.com/microsoft/TypeScript/issues/45566#issuecomment-905057883 https://github.com/microsoft/TypeScript/issues/45566#issuecomment-905057883
  2. https://github.com/microsoft/TypeScript/issues/45566 https://github.com/microsoft/TypeScript/issues/45566
  3. https://developer.mozilla.org/en-US/docs/Web/API/btoa (No deprecation warnings!) https://developer.mozilla.org/en-US/docs/Web/API/btoa (没有弃用警告!)

Still within VS Code, I had a look into the comments for the deprecated btoa(str) function.仍然在 VS Code 中,我查看了已弃用的 btoa(str) 函数的注释。 It suggests the following as a replacement:它建议将以下内容作为替代:

const base64Str = Buffer.from(str, 'utf8').toString('base64');

I am using the follow methods below to encode/decode base64 in modern Javascript ES6.我正在使用以下方法在现代 Javascript ES6 中对 base64 进行编码/解码。 It's nice to wrap these methods in a js utils file, so it's easy to just import them and use.将这些方法包装在一个 js utils 文件中很好,因此只需导入它们并使用它们就很容易。

export const encodeBase64 = (data) => {
    return Buffer.from(data).toString('base64');
}
export const decodeBase64 = (data) => {
    return Buffer.from(data, 'base64').toString('ascii');
}

The Node btoa() and atob() functions are the only ones that have been deprecated. Node btoa() 和 atob() 函数是唯一被弃用的函数。 However, if you're working on the DOM code (front-end) and see this deprecated notice, you can use the window object to get around it.但是,如果您正在处理 DOM 代码(前端)并看到此弃用通知,则可以使用 window 对象来绕过它。

window.atob()

For more info欲了解更多信息

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

相关问题 Javascript-使用atob()和btoa()函数从base64进行编码和解码 - Javascript - code and decode from base64 using atob() and btoa() functions 将二进制数据转换为 base64 不适用于 btoa unescape - Convert binary data to base64 does not work with btoa unescape 使用 Java/Kotlin Base64 解码器解码 JavaScript btoa() 编码的字符串 - Decode JavaScript btoa() encoded string using Java/Kotlin Base64 decoder atob 如何不使用 base64 从 Buffer 转换? - How atob doesn't convert from Buffer with base64? JavaScript atob 不同于 Notepad++ Base64 解码 - JavaScript atob is different than Notepad++ Base64 Decode 在Javascript中将byte []转换为Base64字符串 - Convert byte[] to Base64 string in Javascript 使用javascript将base64字符串转换为图像 - convert base64 string to image with javascript 从AS3返回Base64字符串到Javascript。 这是错误或功能(还是我错了?) - Returning Base64 string from AS3 to Javascript. Is this a bug or feature (or am I wrong?) 如何在javascript中将base64 png字符串转换为base64 svg字符串? - How to convert a base64 png string to base64 svg string in javascript? 将base64图像转换为javascript中的文件或如何使用jquery ajax传递大的base64字符串 - Convert base64 image to file in javascript or how to pass a big base64 string with jquery ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM