0

我正在构建一个 Meteor 应用程序,该应用程序通过 HTTP 请求与桌面客户端通信,带有https://github.com/crazytoad/meteor-collectionapi

桌面客户端以不规则的时间间隔生成图像,我希望 Meteor 站点仅显示最近生成的图像(最好是实时的)。我最初的想法是对带有 base64 图像数据的单例集合使用 PUT 请求,但我不知道如何在 Web 浏览器中将该数据转换为图像。注意:图像都非常小(远小于 1 MB),因此不需要使用 gridFS。

我意识到这个想法可能是完全错误的,所以如果我完全走错了路,请提出更好的行动方案。

4

1 回答 1

3

您需要编写一个中间件来为您的图像提供正确的 MIME 类型。例子:

WebApp.connectHandlers.stack.splice (0, 0, {
  route: '/imageserver',
  handle: function(req, res, next) {

    // Assuming the path is /imageserver/:id, here you get the :id
    var iid = req.url.split('/')[1];

    var item = Images.findOne(iid);

    if(!item) {
      // Image not found
      res.writeHead(404);
      res.end('File not found');
      return;
    }

    // Image found
    res.writeHead(200, {
      'Content-Type': item.type,
    });
    res.write(new Buffer(item.data, 'base64'));
    res.end();

  },

});
于 2014-04-18T07:50:24.757 回答