To convert an instance of a Buffer to base64 in Node.js, you can use the toString() method with the base64 encoding option, like this:

const buffer = Buffer.from('Hello World!', 'utf8');
const base64 = buffer.toString('base64');
console.log(base64); // SGVsbG8gV29ybGQh

In this example, we create a Buffer instance from the string "Hello World!" using the UTF-8 encoding. Then, we call the toString() method on the buffer object with the base64 encoding option. The resulting base64 string is stored in the base64 variable and logged to the console.

Note that the toString() method can also take an optional encoding parameter that specifies the character encoding of the input buffer. If no encoding is specified, the default is utf8

how to convert instanceof Buffer to base64

原文地址: https://www.cveoy.top/t/topic/hoj9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录