提前感谢你们能帮我的忙。我正在尝试使用mongodb、mongoose、gridfs-strea和express在mongodb上存储img文件。我不想将文件存储在文件夹中。我希望gfs.createWriteStream直接从特快专递的app.post获取文件。App.post目前正在通过模式模型将一些字符串保存到mongodb中。路由到app.post的表单包含字符串和img文件的输入。
我尝试这样做(dnimg前端是输入的名称):
dnimgfront:req.pipe(gfs.createWriteStream('req.body.dnimgfront'))我要回去了。
TypeError: Object function Grid(db, mongo) {
if (!(this instanceof Grid)) {
return new Grid(db, mongo);
}
mongo || (mongo = Grid.mongo ? Grid.mongo : undefined);我的问题是,我必须能够存储来自同一个app.post保存函数的img和字符串。有什么想法吗?
我用来连接mongodb的是:
mongoose.connect('mongodb://localhost/veeltaxi');
var con=mongoose.connection;
con.once('open', function(){
console.log('connected to mongodb succesfully!')
});提前谢谢你的帮助。
发布于 2015-01-13 19:54:58
首先,您需要一个而不是gridfs流:
var GridStream = require('gridfs-stream');
var mongodb = require('mongodb');
var gfs = new GridStream(mongoose.connection,mongodb);此时,您可以创建写流和管道:
var writeStream = gfs.createWriteStream({
filename: filename, // the name of the file
content_type: mimetype, // somehow get mimetype from request,
mode: 'w' // ovewrite
});
req.pipe(writeStream);但是,此时Node.js MongoDB驱动程序位于2.0版本,它使用的是流2,所以如果您使用的是mongodb库的版本,则没有理由使用gridfs-stream。请参阅v2.0.13的源代码。 FYI,在更新的驱动程序中实现的流是双工的。(顺便说一句:gridfs流也有自己的问题,虽然积极维护,但并不是很频繁。)
https://stackoverflow.com/questions/27930266
复制相似问题