Node.js从Mongo的GridFS中显示图像

3

我有一个使用Mongo和GridFS存储图像的Node.js应用程序。 我正在尝试通过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 native 1.0.2后,我正在尝试使用以下方法流式传输数据:

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

gridStore.open(function(err, imageStore){ })后,imageStore是一个对象。

该对象与图片存储相关。
2个回答

5

请确保你所使用的驱动程序版本为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);
      })
    })
  });
}

我已将版本更新为1.0.2。但是我得到了一个损坏的图像,在PHP中它正常工作。我已在上面的帖子中更新了代码,谢谢! - dzm
你确定PHP驱动程序工作正常吗?测试代码的方法是使用与你的Mongo服务器一起提供的mongofiles二进制文件上传一个测试文件,然后用你的代码流式传输它。 - christkv

0

环境: express (3.0), mongodb-native-driver (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);

});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接