当前位置: 首页 > 知识库问答 >
问题:

使用NodeJS(Express)将图像转换到Base64

邓高韵
2023-03-14

我正在做一个测试,我需要将一个图像转换为base64以存储在我的DB中

忘记DB,我只需要base64作为字符串。

这是我的表格:

<form action="/cadastrado" method="POST">
   <! -- other irrelevants inputs -->
   <input type="file" name="input_file" id="input_file" onchange="converterParaBase64()" required><br>
   <button type="submit" onclick="base64()">CADASTRAR</button>
</form>

这是我提交表格后的路线:

app.post("/cadastrado", (req, res) => {
    const {input_nome, input_telefone, input_cpf, input_email, input_file} = req.body;

    console.log(req);

    return res.json({
      nome: input_nome,
      telefone: input_telefone,
      cpf: input_cpf,
      email: input_email,
      // base64image: input_file.toBase64 (I need something like this)
    });
  });

我正在使用字符串输入,如nome,telefone,cpf和email(在PT-BR中的变量)

但对于input_file我不能这么说

当我console.log(req.body)时,我会得到以下信息:

json prettyprint-override">{
  input_nome: '1',
  input_telefone: '2',
  input_cpf: '3',
  input_email: '4',
  input_file: 'levia.jpg'
}

我需要将这个levia.png图像转换为base64作为字符串。

我怎么能做到?

共有1个答案

端木狐若
2023-03-14

试试这个

var fs = require('fs');

app.post("/cadastrado", (req, res) => {
    const {input_nome, input_telefone, input_cpf, input_email, input_file} = req.body;

    console.log(req);

var bitmap = fs.readFileSync(input_file);
var base64File = new Buffer(bitmap).toString('base64');

    return res.json({
      nome: input_nome,
      telefone: input_telefone,
      cpf: input_cpf,
      email: input_email,
      base64image: base64File 
    });
  });
 类似资料:
  • 问题内容: 我想将PDF页面转换为图像(PNG,JPEG / JPG或GIF)。我希望它们有整页尺寸。 使用Java如何做到这一点?哪些库可用于实现此目的? 问题答案: 您将需要一个PDF渲染器。市场上有一些或多或少的好工具(ICEPdf,pdfrenderer),但是如果没有,您将不得不依靠外部工具。免费的PDF渲染器也无法渲染嵌入的字体,因此仅适用于创建缩略图(您最终想要的)。 我最喜欢的外部

  • 当使用PDFBox将PDF(可填充)转换为Jpeg时。复选框中的勾号将转换为方框字符 警告[org.apache.pdfbox.rendering.type1glyph2d]代码52(a20)没有字形,字体为ZapfDingbats public static void main(String[]args)引发异常{ 我如何设置字体到PDF图像代码?

  • 我正在尝试使用下面的函数将CGM文件转换为PNG。我一直在得到NullPointerException。 下面是我面临的例外 我尝试调试代码,发现ImageIO不支持CGM格式(CanDecodeInputFilter为我的CGM返回false)。关于如何将CGM转换为PNG图像的任何想法。 PS:我正在使用java 11进行开发。 提前感谢 使现代化 根据回复,我尝试了jcgm库(将核心和图像库

  • 我有申请发邮件。图书馆使用了斯威夫特·梅勒。我已经生成了用于发送电子邮件html字符串,其中包含各种标记,包括标记我可以将img src标记转换为bas64图像吗 至

  • 问题内容: 有没有一种方法可以将JPanel(尚未显示)转换为BufferedImage? 谢谢, 杰夫 问题答案: 在BufferedImage中,您可以创建一个图形对象,可用于在JPanel上调用画图,如下所示: 您可能需要确保首先设置面板的尺寸。

  • 我有一个UIExtView和一个UIImageView,我想把它们转换成一个单独的图像来共享。 SaveImageView是ImageView,我想在这里保存textview的图像。 Textview可以在屏幕上移动它,所以我决定保存它们的最终位置,并将其交给SaveImageView。 首先转换图像中的UItext View并保存他的位置。 然后,我想加入两个Imageview,成一个单一的图像