2012-05-14 107 views
2

我有一个使用Mongo和GridFS存储图像的nodejs应用程序。我试图通过Node.js将这些图像显示到浏览器(使用快速框架)。Node.js显示来自Mongo GridFS的图像

我目前使用:

  res.writeHead(200, {'Content-Type': 'image/jpeg' }); 
      res.end(imageStore.currentChunk.data.buffer, 'binary'); 

imageStore是创建一个新的Gridstore的,并呼吁gridStore.open(...)

 var gridStore = new GridStore(self.collection.db, doc._id, doc.filename, 'r', { 
      chunk_size: doc.chunkSize 
     }); 
     gridStore.open(callback); 

后Gridstore的对象我敢肯定,这是不正确的,它显示了一个破碎的图像。有什么建议么?

谢谢!

编辑:

更新到MongoDB的原生1.0.2后,我试图通过使用流式传输数据:使用gridStore.open(function(err, imageStore){ })

回答

5

确保后

res.contentType("image/jpeg"); 
var imageStream = imageStore.stream(true); 
imageStream.pipe(res); 

imageStore是对象您在驱动程序的1.0.1版本上,并使用http请求的管道来传输数据,下面的示例将它转换为文件。在1.1中,将得到更好的Gridstore的对象将是一个读/写流兼容的对象:)

/** 
* 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); 
     }) 
    }) 
    }); 
} 
+0

我已经更新了我的版本1.0.2。但我得到了一个破碎的图像,在PHP中它工作正常。我已经用代码更新了我的帖子,谢谢! – dzm

+0

你确定php驱动程序能正常工作吗?测试代码的方法是使用monogo服务器附带的二进制mongofiles并上传测试文件,然后使用代码进行流式处理。 – christkv

0

ENV:快递(3.0),MongoDB的本地驱动器(1.2),mongodb的(2.2)

GS - GridStore.writeFile后文件信息

资源 - 快递响应对象

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); 

});