MongoDB GridFs用C#,如何存储图像等文件?
我正在开发一个以 mongodb 作为后端的网络应用程序。我想让用户将图片上传到他们的个人资料中,例如链接的个人资料图片。我正在使用带有 MVC2 的 aspx 页面,并且我读到 GridFs 库用于将大型文件类型存储为二进制文件。我到处寻找如何完成此操作的线索,但 mongodb 没有任何 C# api 或 GridFs C# 的文档。我感到困惑和困惑,真的可以使用另一组大脑。
有人知道如何实际实现一个文件上传控制器,将用户上传的图像存储到 mongodb 集合中吗?谢谢一百万!
我尝试过这种方法的变体,但没有成功。
Database db = mongo.getDB("Blog");
GridFile file = new GridFile(db);
file.Create("image.jpg");
var images = db.GetCollection("images");
images.Insert(file.ToDocument());
I'm developing a web app with mongodb as my back-end. I'd like to have users upload pictures to their profiles like a linked-in profile pic. I'm using an aspx page with MVC2 and I read that GridFs library is used to store large file types as binaries. I've looked everywhere for clues as how this is done, but mongodb doesn't have any documentation for C# api or GridFs C#. I'm baffled and confused, could really use another set of brains.
Anyone one know how to actually implement a file upload controller that stores an image uploaded by a user into a mongodb collection? Thanks a million!
I've tried variations of this to no avail.
Database db = mongo.getDB("Blog");
GridFile file = new GridFile(db);
file.Create("image.jpg");
var images = db.GetCollection("images");
images.Insert(file.ToDocument());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然 2.1 RC-0 驱动程序已经发布,上述答案很快就会过时。
现在可以通过以下方式使用 GridFS 处理 v2.1 MongoDB 中的文件:
这取自 C# 驱动程序测试的差异 此处
The answers above are soon to be outdated now that the 2.1 RC-0 driver has been released.
The way to work with files in v2.1 MongoDB with GridFS can now be done this way:
This is taken from a diff on the C# driver tests here
此示例将允许您将文档绑定到对象
This example will allow you to tie a document to an object
以下示例展示如何保存文件并从 gridfs 读回(使用官方 mongodb 驱动程序):
结果:
文件:
块集合:
希望有所帮助。
Following example show how to save file and read back from gridfs(using official mongodb driver):
Results:
File:
Chunks collection:
Hope this help.