Node.js显示来自Mongo的图像';s网格
我有一个nodejs应用程序,它使用Mongo和GridFS来存储图像。我正试图通过Node.js(使用express framework)将这些图像显示到浏览器中 我目前正在使用:Node.js显示来自Mongo的图像';s网格,node.js,mongodb,gridfs,Node.js,Mongodb,Gridfs,我有一个nodejs应用程序,它使用Mongo和GridFS来存储图像。我正试图通过Node.js(使用express framework)将这些图像显示到浏览器中 我目前正在使用: res.writeHead(200, {'Content-Type': 'image/jpeg' }); res.end(imageStore.currentChunk.data.buffer, 'binary'); imageStore是在创建新的gridStor
res.writeHead(200, {'Content-Type': 'image/jpeg' });
res.end(imageStore.currentChunk.data.buffer, 'binary');
imageStore是在创建新的gridStore并调用gridStore.open(…)之后对gridStore对象的
我肯定这不是正确的方式,它显示的是一个损坏的图像。有什么建议吗
谢谢
编辑:
更新到mongodb native 1.0.2后,我将尝试使用以下方式来流式传输数据:
res.contentType("image/jpeg");
var imageStream = imageStore.stream(true);
imageStream.pipe(res);
imageStore是使用
gridStore.open(函数(err,imageStore){})
后的对象。请确保您使用的是驱动程序的1.0.1,并使用http请求的管道来流式传输数据,下面的示例是对文件执行此操作。在1.1中,它将变得更好,因为gridstore对象将是读/写流兼容的对象:)
环境:express(3.0)、mongodb本机驱动程序(1.2)、mongodb(2.2)
gs-GridStore.writeFile之后的文件信息
res-express响应对象
monogodb.GridStore.read(db, gs["_id"], function(err, data) {
res.setHeader('Content-Type', gs.contentType);
res.setHeader('Content-Length', gs.length);
res.setHeader('Content-Disposition', 'inline; filename="' + gs.filename + '"');
res.end(data);
});
我已将我的版本更新为1.0.2。但是我得到了一个坏的图像,在PHP中它工作得很好。我已经用代码更新了上面的帖子,谢谢!您确定php驱动程序工作正常吗。测试代码的方法是使用monogo服务器附带的二进制Mongofile,上传测试文件,然后将其与代码一起流式传输。
/**
* A simple example showing how to pipe a file stream through from gridfs to a file
*
* @_class gridstore
* @_function stream
* @ignore
*/
exports.shouldCorrectlyPipeAGridFsToAfile = function(test) {
var db = new Db('integration_tests', new Server("127.0.0.1", 27017,
{auto_reconnect: false, poolSize: 1, ssl:useSSL}), {native_parser: native_parser});
// Establish connection to db
db.open(function(err, db) {
// Open a file for writing
var gridStoreWrite = new GridStore(db, "test_gs_read_stream_pipe", "w", {chunkSize:1024});
gridStoreWrite.writeFile("./test/gridstore/test_gs_weird_bug.png", function(err, result) {
// Open the gridStore for reading and pipe to a file
var gridStore = new GridStore(db, "test_gs_read_stream_pipe", "r");
gridStore.open(function(err, gridStore) {
// Grab the read stream
var stream = gridStore.stream(true);
// When the stream is finished close the database
stream.on("end", function(err) {
// Read the original content
var originalData = fs.readFileSync("./test/gridstore/test_gs_weird_bug.png");
// Ensure we are doing writing before attempting to open the file
fs.readFile("./test_gs_weird_bug_streamed.tmp", function(err, streamedData) {
// Compare the data
test.deepEqual(originalData, streamedData);
// Close the database
db.close();
test.done();
});
})
// Create a file write stream
var fileStream = fs.createWriteStream("./test_gs_weird_bug_streamed.tmp");
// Pipe out the data
stream.pipe(fileStream);
})
})
});
}
monogodb.GridStore.read(db, gs["_id"], function(err, data) {
res.setHeader('Content-Type', gs.contentType);
res.setHeader('Content-Length', gs.length);
res.setHeader('Content-Disposition', 'inline; filename="' + gs.filename + '"');
res.end(data);
});