nodejs读取服务器图片,转为base64显示在网页上
const fs = require("fs");
const mineType = require("mime-types");
function imgToBase64(url) {
try {
let imgurl = config.imageUrl;
let imageData = fs.readFileSync(imgurl);
if (!imageData) return "";
let bufferData = Buffer.from(imageData).toString("base64");
let base64 = "data:" + mineType.lookup(imgurl) + ";base64," + bufferData;
return base64;
} catch (error) {
return "";
}
}