【问题标题】:Node.js displaying images from Mongo's GridFSNode.js 显示来自 Mongo 的 GridFS 的图像
【发布时间】: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){ })后的对象

【问题讨论】:

    标签: node.js mongodb gridfs


    【解决方案1】:

    确保您在驱动程序的 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 中它工作正常。我已经用代码更新了我上面的帖子,谢谢!
    • 您确定 php 驱动程序工作正常。测试代码的方法是使用mongo服务器附带的二进制mongofiles并上传测试文件,然后将其与您的代码一起流式传输。
    【解决方案2】:

    env: express (3.0), mongodb-native-driver (1.2), mongodb (2.2)

    gs - GridStore.writeFile 之后的文件信息

    res - 表达响应对象

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

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 2017-06-23
      • 2016-04-27
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      相关资源
      最近更新 更多