【发布时间】:2012-05-22 06:20:55
【问题描述】:
我有一个 nodejs 应用程序,它使用 Mongo 和 GridFS 来存储图像。我正在尝试通过 Node.js(使用 express 框架)将这些图像显示到浏览器。
我目前正在使用:
res.writeHead(200, {'Content-Type': 'image/jpeg' });
res.end(imageStore.currentChunk.data.buffer, 'binary');
imageStore是新建GridStore并调用gridStore.open(...)后的gridStore对象
var gridStore = new GridStore(self.collection.db, doc._id, doc.filename, 'r', {
chunk_size: doc.chunkSize
});
gridStore.open(callback);
我确定这不是正确的方法,它会显示损坏的图像。有什么建议吗?
谢谢!
编辑:
更新到 mongodb 本机 1.0.2 后,我尝试使用以下方式流式传输数据:
res.contentType("image/jpeg");
var imageStream = imageStore.stream(true);
imageStream.pipe(res);
imageStore是使用gridStore.open(function(err, imageStore){ })后的对象
【问题讨论】: