51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

如何在Node.js中将图像转换为Base64。

英文:

How to convert image to base64 in node.js

问题 {#heading}

我需要将 fotoName 转换为 base64,有人可以帮助我吗? 英文:

I tried to convert image that uploaded using postman, this is my code to handle image in controller

const foto = req.files.foto;
const fotoName = foto.name;
res.send(fotoName);

I need to convert fotoName to base64, anybody please help me?

答案1 {#1}

得分: 0

尽管不建议这样做,但以下是您如何执行此操作:

var nameBase64 = Buffer.from(foto.name).toString('base64')
res.send(nameBase64);

请记住,在数据库中保存数据的时候,Base64 不是一个方便的方式。您将会遇到极长的加载时间。 英文:

Altough it isn't recommendet to do, this is how you would do it

var nameBase64 = Buffer.from(foto.name).toString('base64')
res.send(nameBase64);

Keep in mind, base64 is not a convinient way to save data in f. e. a database . You will encounter huge loading times.

答案2 {#2}

得分: 0

base64不建议在数据库中使用,因为它可能会增加加载和处理时间。然而,你可以像这样尝试使用:

// 将图像数据转换为base64
const base64Image = data.toString('base64');

res.send(base64Image);

英文:

base64 is not recommended in database because it may actually increase the loading and processing times. However you can you can try it like this:

// Convert the image data to base64
const base64Image = data.toString('base64');
`res.send(base64Image);
`


赞(1)
未经允许不得转载:工具盒子 » 如何在Node.js中将图像转换为Base64。