【问题标题】:nodejs base64 image response is not coming as image object. when adding header {'Content-Type': 'image/jpeg or png'}nodejs base64 图像响应不是作为图像对象出现的。添加标题时 {'Content-Type': 'image/jpeg or png'}
【发布时间】:2018-10-10 10:27:12
【问题描述】:

当我在服务器端生成图像并获取 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();

【问题讨论】:

    标签: node.js fabricjs node-canvas


    【解决方案1】:

    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")
    

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2022-06-30
      • 2021-11-08
      • 2018-02-18
      • 2021-06-28
      相关资源
      最近更新 更多