[英]How can I convert image buffer to string base64?
我正在使用 Filepond 库将图像上传到使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z 的 MongoDB。 我想使用弹出功能在地图的标记中显示图像。 图片我正在恢复格式
{ location:
{ type: 'Point',
coordinates: [ -86.34164, 39.81514 ],
formattedAddress: 'Lucas Oil Raceway, Brownsburg, IN 46234, US' },
_id: 5f10a1867435d3f048a14acc,
storeId: '009',
dateCompleted: 2020-07-01T04:00:00.000Z,
description:
' setup datguard. backups, flashack logs and monitoring',
createdAt: 2020-07-16T18:50:46.997Z,
storeImage:
Binary {
_bsontype: 'Binary',
sub_type: 0,
position: 3569671,
buffer:
<Buffer ff d8 ff e0 00 10 4a....
storeImageType: 'image/jpeg',
__v: 0 }
我能够显示除图像之外的属性的 rest。 有没有办法可以将此图像缓冲区转换为字符串,然后设置为 map 属性。
stores = data.data.map(store => {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [
store.location.coordinates[0],
store.location.coordinates[1]
]
},
properties: {
storeId: store.storeId,
dateCompleted: store.dateCompleted,
description: store.description,
image:`data:${store.storeImageType};charset=utf-8;base64,${store.storeImage.data.toString('base64')}`,
##with this i see image content as "data:image/jpeg;charset=utf;base64,233,4444,555,67 icon: 'shop' } }; });
function setMarkers(stores) {
//add markers to the map
stores.forEach(function (marker) {
// create a HTML element for each feature
var el = document.createElement('div-marker');
el.className = 'marker';
// make a marker for each feature and add to the map
var mkr = new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.setPopup(new mapboxgl.Popup({
offset: 25
}) // add popups this is where i need to set image
.setHTML("<img src='" + marker.properties.image + "' width='160' />"))
.addTo(map);
mkrs.push(mkr);
});
}
所有这些代码都在使用 NodeJS Express 的公共 javascript 文件夹中。
从您发布的格式来看,图像缓冲区似乎位于store.storeImage.buffer
中。 storeImage.data
必须是一个将其转换为其他东西的getter - 从它的外观来看,可能是一个数组?
尝试使用store.storeImage.buffer.toString('base64')
- 将'base64'
传递给toString
是缓冲区的一个特性,其他类型的处理方式不同。
编辑:显然store.storeImage
本身就是缓冲区。 所以你应该简单地使用store.storeImage.toString('base64')
编辑:尽管实际上并未提供 node.js 缓冲区 object,但显然它将自己描述为缓冲区。 我建议将数组转换为缓冲区,然后编码为 base64: Buffer.from(store.storeImage.data).toString('base64')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.