[英]Image to Base64 with NodeJS (Express)
I'm doing a test, and I need to convert an image to base64 to store in my DB我正在做测试,我需要将图像转换为 base64 以存储在我的数据库中
forget about DB, I just need the base64 as a string.忘记数据库,我只需要 base64 作为字符串。
This is my form:这是我的表格:
<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>
This is my route after submit the 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)
});
});
I'm doing greate about using the strings inputs, like nome, telefone, cpf and email (variables in PT-BR)我在使用字符串输入方面做得很好,例如 nome、telefone、cpf 和 email(PT-BR 中的变量)
But I can't say the same with input_file
但我不能对input_file
说同样的话
When I console.log(req.body) i get this:当我 console.log(req.body) 我得到这个:
{
input_nome: '1',
input_telefone: '2',
input_cpf: '3',
input_email: '4',
input_file: 'levia.jpg'
}
I need to convert this levia.png image into base64 as a string.我需要将此 levia.png 图像转换为 base64 作为字符串。
How I can do it?我该怎么做?
UPDATE 1: Seems like the image in json, is just the file name.更新 1:似乎 json 中的图像只是文件名。 It is possible to convert in client-side and after send to server-side?可以在客户端和发送到服务器端之后进行转换吗?
Try this尝试这个
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
});
});
so I figured out how to resolve my problem.所以我想出了如何解决我的问题。
Kind of vicarious what I did.有点替代我的所作所为。 I don't know if in future this can result in a problem, so I want to share with you.不知道以后会不会出现问题,所以想和大家分享一下。
Basically I create a input
and edit his value in client side.基本上我创建一个input
并在客户端编辑他的值。 Of course, I don't want a big input with a Base64 code messing with my poor HTML, so I styled with display: none
.当然,我不想要一个 Base64 代码搞乱我可怜的 HTML 的大输入,所以我用display: none
设置样式。 This input is inside the form, and it is sent to my req.body
.此输入在表单内,并发送到我的req.body
。
Snippets片段
index.html page with form: index.html 页面格式:
...
<input type="file" id="input_file" name="input_file" onchange="convBase64()" required />
...
script.js with some logic script.js 有一些逻辑
...
document.getElementById('input_base64').value = `${stringBase64}`;
...
css to hide the input with encoded image: css 用编码图像隐藏输入:
#input_base64 {
display: none;
}
This invisible input is inside the form.这个不可见的输入在表单内部。 When the form is submitted, the input value is sent to body of request.提交表单时,输入值将发送到请求正文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.