node.js - nodejs base64 图像响应不是作为图像对象出现的。添加标题时 {'Content-Type': 'image/jpeg or png'}
问题描述
当我在服务器端生成图像并获取 base64 图像字符串时。
我想作为图像对象发送。它通过创建图像流与节点画布正常工作。
let stream = this.canvas.jpegStream({progressive: true});
this.res.writeHead(200, {
'Content-Type': 'image/jpeg'
});
stream.on('data', (chunk) => {
this.res.write(chunk);
});
stream.on('end', () => {
this.res.end();
});
// Working perfectly...!
但是当我使用fabric.js 时,它返回的是直接base64 图像,而不是像node-canvas 这样的base64 流。当我发送 base64 作为响应时,图像变为空白。
this.res.writeHead(200, {
'Content-Type': 'image/png'
});
this.res.write(this.fabricCanvas.toDataURL());
this.res.end();
解决方案
Content-Type: image/png
不是与data:
URL 一起使用的正确标头。
您可以尝试以下方法之一:
// Preferred: returns a PNG-encoded image buffer
this.fabricCanvas.toBuffer()
// Wasteful - goes from binary to base64 and back
Buffer.from(this.fabricCanvas.toDataURL().split(",")[1], "base64")
推荐阅读
- python - 如何分离只需要运行一次即可在数据库服务上进行一些数据准备的 docker 应用程序的一部分?
- discord - 如何检查用户是否提到了某人或只是写了胡言乱语
- r - 如何将一组数字分配给变量,但该组数字将随机生成?
- php - 流明:缓存与数据库
- flutter - 无法使用 PdfDocument.openFile(path) 从本地存储打开 pdf - Flutter
- swift - 在导航堆栈 SwiftUI 中显示带有按钮的工作表
- numpy - 带有浮点错误的 Numpy setdiff1d
- mongodb - mongodb 数字与 _id 索引的性能?
- python-3.x - Google KickStarter Round F 2021 #1 解决方案缺陷
- typescript - 计算值无法识别道具对象