我正试图弄明白如何将图像直接发布到GridFS,而不首先将其作为临时文件存储在服务器上的任何位置.

我正在使用Postman(chrome ext.)发布一个文件,并使用以下工具将此帖子存储为文件:

req.pipe(fs.createWriteStream('./test.png'));

当从服务器上的文件创建readStream时,我还能够直接从readStream存储到GridFS.(参见代码)

我有下面的saveFromReq.js个文件,用来监听帖子,基本上就是把它传给savePic.js.

saveFromReq.js:

var express = require('express');
var app = express();
var savePic = require('./savePic');
var fs = require('fs');
var GridStore = require('mongodb').GridStore;
var pic = './square.png';
var picID;



//When the following

 //var pic = fs.createReadStream('./square.png', {autoClose: true});

//is not commented out, and 'req' is replaced with 'pic' in the savePic function,
//the file square.png is stored correctly to GridFS

app.post('/picture', function(req, res){

    savePic(req, function(id){});
    res.writeHead(200, {'Content-Type': 'text' });
    res.end("Sucsess!\n");

});

app.listen(process.env.PORT || 3413);

savePic.js:

var savePic = function(req, callback){


var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');
    fs = require('fs');

    //When the following 

      //req.pipe(fs.createWriteStream('./test.png'));

    //is not commented out, the correct image is stored to test.png, and 
    //the sequence after req.on("data"... starts
    //(That sequence does not start at all when this is commented out..)

var fileId = new ObjectID();
var db = new Db('testDB', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {


  var gridStore = new GridStore(db, 'test', 'w');

  //open 
  gridStore.open(function(err, gridStore) {
    console.log("opened");


    req.on("data", function (data) {
        console.log("data recieved");
            gridStore.write(data, function (err, gridStore) {
                if (err) {
                    console.log("error writing file");
                }
            });
        });
     req.on("end", function () {
            gridStore.close(function (err, gridStore) {
                if (!err) {
                    console.log("The file has been stored to database.");
                    db.close();
                }
            });
        });
           req.pipe(gridStore);


      });




});
callback(fileId);
};
module.exports = savePic;

任何帮助都将不胜感激!

推荐答案

gridfs-stream让这变得很容易:

// `gfs` is a gridfs-stream instance
app.post('/picture', function(req, res) {
  req.pipe(gfs.createWriteStream({
    filename: 'test'
  }));
  res.send("Success!");
});

Mongodb相关问答推荐

Mongo 将一个数组链接到另一个数组

将数据从一个集合插入另一个集合的聚合

如何判断 MongoDB 的两个数组中是否存在值?

找到一个用户,然后使用 MongoDB 根据他们的总分获得他们的排名

如何在 Spring-Data-MongoDB 中使用 $facet、$addFields 和 $function

MongoDB - 文档中多个数组大小的总和

使用 mongodb 时是否需要规范化数据库?

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

Meteor mongo 驱动程序可以处理 $each 和 $position 运算符吗?

MongoDB 2.1 聚合框架总和匹配名称的数组元素

Mongoose 和新架构:返回ReferenceError: Schema is not defined

MongoDB 3 Java判断集合是否存在

如何在mongoose中加入两个集合

MongoDB Compass 过滤器(查询)

如何使用 MongoDB 以编程方式预拆分基于 GUID 的分片键

Mongodb:为什么 show dbs 不显示我的数据库?

用于嵌入式集合的 MongoDB 首选模式.文档与数组

Mongodb Atlas:管理员未授权执行命令

如何通过键名从 mongoDB 中检索值?

如何在 Mongoid 中引用嵌入的文档?