【问题标题】:Nodejs serve base64 as ImageNodejs 将 base64 用作图像
【发布时间】:2021-10-26 05:37:30
【问题描述】:

我正在尝试将 base64 字符串作为带有 image/png 标头的图像提供。标题设置正确,但没有显示图像,我只能看到一个空白屏幕。这是代码:

request('someCustomLink', function (error, response, body) {
// someCustomLink gives the base64 string
        var img = new Buffer(body, 'base64');
        res.writeHead(200, {
            'Content-Type': 'image/png',
            'Content-Length': img.length
        });
        res.end(img);         
});

this 是我找到此解决方案所遵循的链接。

任何帮助将不胜感激。

编辑 这是我的 someCustomLink 的响应标头(它可能有助于理解问题)

Accept-Ranges:bytes
Content-Length:128778
Content-Type:image-jpeg
Date:Thu, 21 Dec 2017 06:03:52 GMT
ETag:"edc04d469779108860478b361b7427d7"
Last-Modified:Mon, 11 Dec 2017 08:54:32 GMT
Server:AmazonS3
x-amz-id-2:QlR39g0vC5CeOo6fdKzX9uFB+uiOtj61c0LKW2rQLcCPWllcKyfbpg93Yido+vZfzMzB3lmhbNQ=
x-amz-request-id:3EC77634D9A05371

这是获取请求

var request = require('request').defaults({ encoding: null });

app.get('/thumb/:thumbLink', function(req, res) {


        request('https://s3.amazonaws.com/my-trybucket/projectImages/'+req.params.thumbLink, function (error, response, body) {
            //console.log('error:', error); // Print the error if one occurred
            //console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
            //console.log('body:', body); // Print the HTML for the Google homepage.
            var img = new Buffer(body, 'base64');
            res.writeHead(200, {
                'Content-Type': 'image/png'

            });
            res.end(img);
        });
    });

-谢谢

【问题讨论】:

  • 为什么要为您提供 base64 PNG?这很不寻常。
  • 我有一个 s3 存储桶,其中包含 base64 PNG 格式的图像。
  • 您是否尝试记录body
  • 是的,试过了。它提供 base64 字符串...
  • 这是我正在使用的链接s3.amazonaws.com/my-trybucket/projectImages/…

标签: javascript node.js


【解决方案1】:

您收到的回复包含data:image/png;base64,。在创建 Buffer 之前,您已将其删除。请参阅下面的示例

request('https://s3.amazonaws.com/my-trybucket/projectImages/e31492f7314837a22a64395eee7cedfd', function(error, response, body) {
    var img = new Buffer(body.split(',')[1], 'base64');
    res.writeHead(200, {
      'Content-Type': 'image/png',
      'Content-Length': img.length 
    });
    res.end(img);
})

【讨论】:

  • 但是这个 Google 徽标不是 base64。我有 customLink 服务 base64 字符串,我需要将其转换为图像。还尝试初始化请求,因为您告诉我结果是一样的,我看到的是一个空白图像
  • 你能发布一个功能示例吗?
  • 添加了功能获取请求。你可以在你的系统上复制它。在上面的评论中找到链接
  • 更新了答案。同时删除.defaults({ encoding: null });,如前所述。
  • Buffer() 由于安全性和可用性问题而被弃用。请改用Buffer.alloc()Buffer.allocUnsafe()Buffer.from() 方法。
【解决方案2】:

在这个特定问题上也停留了很长时间,只是发现发送从 s3 返回的实际数据 Body 实际上足以显示图像。 这是我的实现(在打字稿中):

// a method in my Bucket class

async getObject(key: string) {
    const params = {
      Bucket: this.bucket, //my bucket name set in my constructor
      Key: key,
    };
    return new Promise((resolve, reject) => {
      s3.getObject(params, (err, data) => {
        if (err) reject(err);
        if(data) resolve(data.Body);
      });
    });
  }

// the get request route

app.get(
  "url/:folder/:id",
  async (req: Request, res: Response) => {
    const { folder, id } = req.params;
    const image = (await new Bucket().getObject(`${folder}/${id}`)) as Buffer;
    res.send(image);
  }
);

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2021-08-31
    • 1970-01-01
    • 2011-12-27
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多