用gridfs-stream直接在mongodb中保存文件 [英] stored files directly in mongodb with gridfs-stream
问题描述
我需要保存一些文件,如:图像,视频,pdf ...到mongodb,所以我使用gridfs-stream和express.js
var file = req.files.file;
req.pipe(gfs.createWriteStream({
filename:file.originalname,
mode:w,
chunkSize:1024 * 4,
content_type:file .mimetype,
root:fs
})
res.send(200);
测试我使用邮递员,并设置一个POST请求这种方式:
pre $ POST / fs / upload HTTP / 1.1
Host:localhost:5000
Cache-Control:no-cache
---- WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition:form-data; name = file; filename =epic.png
Content-Type:image / png
---- WebKitFormBoundaryE19zNvXGzXaLvS5C
问题是,这种方式只是存储文件的数据:
{
_id:ObjectId(54d14ec5b102fe401519a3c1),
filename:epic.png,
contentType:image / png,
length:0,
chunkSize:4096,
uploadDate:ISODate(2015-02-03T22:42:14.730Z),
别名es:null,
metadata:null,
md5:993fb9ce262a96a81c79a38106147e95
}
而不是内容我的意思是de二进制数据为它,进入mongodb是存储它的propety长度等于0,因为在fs.chucks没有任何块。
解决方案
阅读博客发现直接在数据库中使用express.js,gridfs-stream.js和multer中间件流式传输数据的答案: / p>
var multer = require('multer');
$ b app.post('/ fs / upload',multer({
上传:null,//获取上传过程
onFileUploadStart:function(file){
//使用WritableStream设置上传
this.upload = gfs.createWriteStream({
filename:file.originalname,
mode:w,
chunkSize:1024 * 4,
content_type:file.mimetype,
root:fs
});
},
onFileUploadData:function(file,data ){
//将卡盘放入db
this.upload.write(data);
},
onFileUploadComplete:function(file){
//结束进程
this.upload.on('drain',function(){
this.upload.end();
});
} $ b $ (b)},function(req,res){
res.sendStatus(200);
});
用于测试:
app.route('/ fs / download /:file')。get(function(req,res){
var readstream = gfs.createReadStream({_ id:req.params.file });
readstream.pipe(res);
});
I need save some files like: images,video,pdf... into mongodb, so i use gridfs-stream and express.js
var file = req.files.file;
req.pipe(gfs.createWriteStream({
filename:file.originalname,
mode:"w",
chunkSize:1024*4,
content_type:file.mimetype,
root:"fs"
})
res.send(200);
for testing i use postman and set an POST request this way:
POST /fs/upload HTTP/1.1
Host: localhost:5000
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="epic.png"
Content-Type: image/png
----WebKitFormBoundaryE19zNvXGzXaLvS5C
the problem is that this way just store the data of the file:
{
"_id" : ObjectId("54d14ec5b102fe401519a3c1"),
"filename" : "epic.png",
"contentType" : "image/png",
"length" : 0,
"chunkSize" : 4096,
"uploadDate" : ISODate("2015-02-03T22:42:14.730Z"),
"aliases" : null,
"metadata" : null,
"md5" : "993fb9ce262a96a81c79a38106147e95"
}
but not the content i mean de binary data for it, into mongodb is store for it the propety length is equal to 0, bacause has not any chunks in fs.chucks.
Reading in blogs found the answer to streaming data directly at database with express.js,gridfs-stream.js and multer middleware that way:
var multer = require('multer');
app.post('/fs/upload', multer({
upload: null,// take uploading process
onFileUploadStart: function (file) {
//set upload with WritableStream
this.upload = gfs.createWriteStream({
filename: file.originalname,
mode: "w",
chunkSize: 1024*4,
content_type: file.mimetype,
root: "fs"
});
},
onFileUploadData: function (file, data) {
//put the chucks into db
this.upload.write(data);
},
onFileUploadComplete: function (file) {
//end process
this.upload.on('drain', function () {
this.upload.end();
});
}
}), function (req, res) {
res.sendStatus(200);
});
For testing this:
app.route('/fs/download/:file').get(function (req, res) {
var readstream = gfs.createReadStream({_id: req.params.file});
readstream.pipe(res);
});
这篇关于用gridfs-stream直接在mongodb中保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!