Feathers.js - 内容类型 - 获取

Feathers.js - Content Type - GET

我正在尝试通过 GET 方法从我的羽毛服务器获取图片。正在尝试通过

调用服务器

http://localhost:3030/uploads/945fdcbc5ef41f8d301c14d8cfb3c4ae536f4bffc0338091043ec5cf27b9bcff.png

应该return一张图片。相反,我得到这样的回应:

{"id":"945fdcbc5ef41f8d301c14d8cfb3c4ae536f4bffc0338091043ec5cf27b9bcff.png","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACoCAMAAABt9SM9AAABqlBMVEV/f38AAAD///....... " }

我的问题是:我得到 Json 响应并且必须将 "responded" 内容类型更改为 data:image/png 以直接在浏览器?

您可以使用 <img src="${data.uri}"> 直接在网站上显示此图像。

也可以在 custom service middleware 中更改内容类型和响应,将数据 URI 转换为缓冲区:

app.use('/uploads', uploadService, (req, res) => {
  // res.data is the response. We need only the base64 payload without the prefix
  const base64Data = res.data.uri.replace('data:image/png;base64,', '');
  const buffer = Buffer.from(base64Data, 'base64');

  res.set('Content-Type', 'image/png');
  res.send(buffer);
});

现在访问 URL 时应该会显示图像。

感谢@Daff!我的代码现在可以使用此解决方案:

app.use(
    "/uploads",uploadservice.....,
     (req, res, next) => {
      const base64Data = res.data.uri.replace("data:image/png;base64,", "");
      const buffer = Buffer.from(base64Data, "base64");
      res.set("Content-Type", "image/png");
      res.send(buffer);
      next();
    }
  );