database 将图像存储在 MongoDB 数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4796914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store images in a MongoDB database
提问by Chani
How can I store images in a MongoDB database rather than just text? Can I create an array of images in a MongoDB database? Will it be possible to do the same for videos?
如何在 MongoDB 数据库中存储图像而不仅仅是文本?我可以在 MongoDB 数据库中创建一组图像吗?可以对视频做同样的事情吗?
采纳答案by Gates VP
回答by Mike Causer
"You should always use GridFS for storing files larger than 16MB" - When should I use GridFS?
“您应该始终使用 GridFS 来存储大于 16MB 的文件” -我什么时候应该使用 GridFS?
MongoDB BSON documentsare capped at 16 MB. So if the total size of your array of files is less than that, you may store them directly in your document using the BinDatadata type.
MongoDB BSON 文档的上限为 16 MB。因此,如果文件数组的总大小小于该值,则可以使用BinData数据类型将它们直接存储在文档中。
Videos, images, PDFs, spreadsheets, etc. - it doesn't matter, they are all treated the same. It's up to your application to return an appropriate content type header to display them.
视频、图像、PDF、电子表格等 - 没关系,它们都被同等对待。由您的应用程序返回适当的内容类型标头以显示它们。
Check out the GridFS documentationfor more details.
查看GridFS 文档以获取更多详细信息。
回答by victoriza
You can try this one:
你可以试试这个:
String newFileName = "my-image";
File imageFile = new File("/users/victor/images/image.png");
GridFS gfsPhoto = new GridFS(db, "photo");
GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
gfsFile.setFilename(newFileName);
gfsFile.save();
回答by Mick Cullen
http://blog.mongodb.org/post/183689081/storing-large-objects-and-files-in-mongodb
http://blog.mongodb.org/post/183689081/storing-large-objects-and-files-in-mongodb
There is a Mongoose plugin available on NPM called mongoose-file. It lets you add a file field to a Mongoose Schema for file upload. I have never used it but it might prove useful. If the images are very small you could Base64 encode them and save the string to the database.
NPM 上有一个名为 mongoose-file 的 Mongoose 插件。它允许您将文件字段添加到 Mongoose Schema 以进行文件上传。我从未使用过它,但它可能证明是有用的。如果图像非常小,您可以对它们进行 Base64 编码并将字符串保存到数据库中。
Storing some small (under 1MB) files with MongoDB in NodeJS WITHOUT GridFS
回答by Tharindu
You can use the bin data type if you are using any scripting language to store files/images in MongoDB. Bin data is developed to store small size of files.
如果您使用任何脚本语言在 MongoDB 中存储文件/图像,则可以使用 bin 数据类型。Bin 数据被开发用于存储小尺寸的文件。
Refer to your scripting language driver. For PHP, click here.
请参阅您的脚本语言驱动程序。对于 PHP,请单击此处。
回答by Samkit Shah
install below libraries
安装下面的库
var express = require(‘express');
var fs = require(‘fs');
var mongoose = require(‘mongoose');
var Schema = mongoose.Schema;
var multer = require('multer');
connect ur mongo db :
连接你的 mongo 数据库:
mongoose.connect(‘url_here');
Define database Schema
定义数据库模式
var Item = new ItemSchema(
{ img:
{ data: Buffer, contentType: String }
}
);
var Item = mongoose.model('Clothes',ItemSchema);
using the middleware Multer to upload the photo on the server side.
使用中间件Multer在服务器端上传照片。
app.use(multer({ dest: ‘./uploads/',
rename: function (fieldname, filename) {
return filename;
},
}));
post req to our db
将请求发布到我们的数据库
app.post(‘/api/photo',function(req,res){
var newItem = new Item();
newItem.img.data = fs.readFileSync(req.files.userPhoto.path)
newItem.img.contentType = ‘image/png';
newItem.save();
});
回答by AshvinG
var upload = multer({dest: "./uploads"});
var mongo = require('mongodb');
var Grid = require("gridfs-stream");
Grid.mongo = mongo;
router.post('/:id', upload.array('photos', 200), function(req, res, next){
gfs = Grid(db);
var ss = req.files;
for(var j=0; j<ss.length; j++){
var originalName = ss[j].originalname;
var filename = ss[j].filename;
var writestream = gfs.createWriteStream({
filename: originalName
});
fs.createReadStream("./uploads/" + filename).pipe(writestream);
}
});
In your view:
在您看来:
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="photos">
With this code you can add single as well as multiple images in MongoDB.
使用此代码,您可以在 MongoDB 中添加单个和多个图像。
回答by harikesh jaiswal
This worked for me,
这对我有用,
- Go in This directory In CMD :- C:\Program Files\MongoDB\Server\4.0\bin
- execute following command :- mongofiles put image.jpg
- for select files we use :- db.fs.files.find.pretty()
- 进入这个目录在 CMD 中:- C:\Program Files\MongoDB\Server\4.0\bin
- 执行以下命令:- mongofiles put image.jpg
- 对于我们使用的选择文件:- db.fs.files.find.pretty()