【问题标题】:Feathers.js - Content Type - GETFeathers.js - 内容类型 - GET
【发布时间】:2019-03-22 09:17:19
【问题描述】:

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

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

应该返回一个图像。相反,我得到如下回应:

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

我的问题是:我得到一个 Json 响应并且必须将“响应的”内容类型更改为 data:image/png 以直接在浏览器中查看图片是否正确?

【问题讨论】:

    标签: express feathersjs


    【解决方案1】:

    您可以使用<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 时应该会显示一张图片。

    【讨论】:

      【解决方案2】:

      感谢@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();
          }
        );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-04
        • 1970-01-01
        • 1970-01-01
        • 2021-07-31
        • 2012-04-23
        • 1970-01-01
        • 2011-03-07
        相关资源
        最近更新 更多