[英]How do I convert an image from a NodeJS Buffer object to base64 in JavaScript so it can be used in the <img> src attribute?
[英]How to return Base64 which I can use with <img src="renderedImg(photo)"/>
我没有找到答案。 同样的问题是如何在 JavaScript 中将文件转换为 base64? .
previewPhoto (photo, id) {
const preview = document.getElementById(id)
const reader = new FileReader()
reader.onloadend = function () {
preview.src = reader.result
}
reader.readAsDataURL(photo)
},
在那里,我只是用 querySelector 替换元素中的 src 值。 但在可能的情况下,我需要这样的东西:
previewPhoto (photo) {
const readyToSrcImg = fileToImg(photo)
return readyToSrcImg
},
有人知道这个案子吗?
不要使用 FileReader 将图像预览为 base64 - 它会浪费时间进行编码/解码并使用更多 memory - 意思是:它更慢。
使用URL.createObjectURL(file)
创建指向原始二进制文件的引用链接
previewPhoto (photo, id) {
const preview = document.getElementById(id)
preview.src = URL.createObjectURL(photo)
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.