0

我通过像这样使用在mongodb中存储了更多图像

exports.save = function (input, image, callback)
{
    db.collection("articles", function (error, collection)
    {
      collection.save(input, {safe: true}, callback);
    });
}

我已经从数据库中检索了所有图像。

find().toArray(function(err,result)
{
     console.log(result.length)//length is 5
     for(var i=0;i<results.length;i++)
     {
                res.contentType(results[i].imageType);
                res.end(results[i].image.buffer, "binary");
     }
});

如何将此图像转换为二进制以及如何向客户端发送响应。我尝试过 for 循环,但出现此错误无法在发送资源后设置标头............如何解决

4

2 回答 2

0

我想,没有办法在一个响应中发送多个图像。但是也许您可以尝试在循环中以某种方式合并这些图像,然后发送一个大图像,该图像将由您合并在一起的 N 个图像组成。

于 2013-04-29T13:30:38.017 回答
0

I have just had a similar problem. You need to pass your 'results' array to a Jade template (for example) or manually create an HTML page with the images that you find (not the data) - rather virtual links to the actual images.

res.render('image_list.jade', {results:results});

Then your template might look like :

block content
  div.container
   header
    h1 Images in a list
   div#images
    - each result in results
   div.myimage
     img(src='http://localhost:3000/oneimage'+result._id)

In your app.js you would need to provide a route /oneimage that returns a single image (data) as you have done in your code (i.e replace findAll(...) with findOne(...., id, ...)

于 2013-10-15T16:23:24.317 回答