【发布时间】:2021-07-19 23:35:00
【问题描述】:
我正在做测试,我需要将图像转换为 base64 以存储在我的数据库中
忘记 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) 我得到这个:
{
input_nome: '1',
input_telefone: '2',
input_cpf: '3',
input_email: '4',
input_file: 'levia.jpg'
}
我需要把这个 levia.png 图片转换成 base64 作为字符串。
我该怎么做?
更新 1: 看起来像json中的图像,只是文件名。 是否可以在客户端和发送到服务器端后进行转换?
【问题讨论】:
-
查看this response,我自己将它用于一个项目。不过,您需要先将输入图像作为画布插入,不幸的是,我没有足够的时间深入了解细节。
-
你需要base64的文件名吗?还是您需要此名称为 base64 的文件内容?
-
@BronislavRůžička 我需要内容图像,例如编码为 base64 的人的图片
-
@Victorbvn 我知道画布,但是对于画布,我需要使用 DOM 元素。那时不要使用 NodeJS。或者,如何将数据传输到另一个 javascript 文件中的字符串?我的 script.js 处理 html,而我的索引正在处理我的 express 服务器。
-
@Victorbvn 一切正常
标签: javascript html node.js image base64