我的Express应用程序正在从浏览器接收base64编码的PNG(从画布生成的toDataURL()),并将其写入一个文件。但该文件不是有效的图像文件,“file”实用程序只是将其标识为“数据”。
var body = req.rawBody,
base64Data = body.replace(/^data:image\/png;base64,/,""),
binaryData = new Buffer(base64Data, 'base64').toString('binary');
require("fs").writeFile("out.png", binaryData, "binary", function(err) {
console.log(err); // writes out file without error, but it's not a valid image
});
我还必须保存Base64编码的图像,这些图像是数据url的一部分,所以我最终制作了一个小的npm模块来做这件事,以防我(或其他人)将来需要再次这样做。叫做ba64。
简单地说,它接受一个带有Base64编码图像的数据URL,并将图像保存到您的文件系统中。它可以同步或异步保存。它还有两个辅助函数,一个用于获取图像的文件扩展名,另一个用于从数据中分离Base64编码:方案前缀。
这里有一个例子:
var ba64 = require("ba64"),
data_url = "data:image/jpeg;base64,[Base64 encoded image goes here]";
// Save the image synchronously.
ba64.writeImageSync("myimage", data_url); // Saves myimage.jpeg.
// Or save the image asynchronously.
ba64.writeImage("myimage", data_url, function(err){
if (err) throw err;
console.log("Image saved successfully");
// do stuff
});
安装:npm i ba64 -S。Repo在GitHub上:https://github.com/HarryStevens/ba64。
P.S.后来我想到ba64可能是一个不好的模块名称,因为人们可能认为它进行Base64编码和解码,但事实并非如此(有很多模块已经这样做了)。哦。
非常简单
const path = require('path');
const { readFile, stat, writeFile } = require("fs/promises");
(async () => {
try {
const contents = await readFile(path.join(__dirname, 'clau.jpg'), { encoding: 'base64' });
console.log(contents);
await writeFile(path.join(__dirname, 'claumia.jpg'), Buffer.from(contents, 'base64'));
} catch (error) {
console.log(error)
}
})()
下面的函数保存文件,只要传递你的base64文件,它返回文件名保存在DB中。
import fs from 'fs';
const uuid = require('uuid/v1');
/*Download the base64 image in the server and returns the filename and path of image.*/
function saveImage(baseImage) {
/*path of the folder where your project is saved. (In my case i got it from config file, root path of project).*/
const uploadPath = "/home/documents/project";
//path of folder where you want to save the image.
const localPath = `${uploadPath}/uploads/images/`;
//Find extension of file
const ext = baseImage.substring(baseImage.indexOf("/")+1, baseImage.indexOf(";base64"));
const fileType = baseImage.substring("data:".length,baseImage.indexOf("/"));
//Forming regex to extract base64 data of file.
const regex = new RegExp(`^data:${fileType}\/${ext};base64,`, 'gi');
//Extract base64 data.
const base64Data = baseImage.replace(regex, "");
const filename = `${uuid()}.${ext}`;
//Check that if directory is present or not.
if(!fs.existsSync(`${uploadPath}/uploads/`)) {
fs.mkdirSync(`${uploadPath}/uploads/`);
}
if (!fs.existsSync(localPath)) {
fs.mkdirSync(localPath);
}
fs.writeFileSync(localPath+filename, base64Data, 'base64');
return filename;
}
简单的方法将base64图像转换为文件,并保存为一些随机id或名称。
// to create some random id or name for your image name
const imgname = new Date().getTime().toString();
// to declare some path to store your converted image
const path = yourpath.png
// image takes from body which you uploaded
const imgdata = req.body.image;
// to convert base64 format into random filename
const base64Data = imgdata.replace(/^data:([A-Za-z-+/]+);base64,/, '');
fs.writeFile(path, base64Data, 'base64', (err) => {
console.log(err);
});
// assigning converted image into your database
req.body.coverImage = imgname
我认为你转换的数据比你需要的多一点。一旦您使用适当的编码创建了缓冲区,您只需要将缓冲区写入文件。
var base64Data = req.rawBody.replace(/^data:image\/png;base64,/, "");
require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
console.log(err);
});
新缓冲区(…, 'base64')将通过将输入解释为base64编码的字符串,将输入字符串转换为Buffer,这只是一个字节数组。然后你就可以把这个字节数组写到文件中。
更新
正如评论中提到的,req。rawBody不再是一个东西。如果您正在使用express/connect,那么您应该使用bodyParser()中间件并使用req。如果您使用标准Node来执行此操作,那么您需要聚合传入的数据事件Buffer对象,并在结束回调中进行图像数据解析。