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

huangapple go评论125阅读模式
英文:

How to convert image to base64 in node.js

问题

我需要将 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

得分: 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

得分: 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);

huangapple
  • 本文由 发表于 2023年8月10日 22:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定