MongoDB的GridFS的用C#,如何存储文件,如图像? [英] MongoDB GridFs with C#, how to store files such as images?

查看:983
本文介绍了MongoDB的GridFS的用C#,如何存储文件,如图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展与MongoDB中为我后端的Web应用程序。我想有用户上传图片到他们的个人资料如链接的用户照片。我使用的是与MVC2一个aspx页面,我读了GridFS的库用于存储大量的文件类型为二进制文件。我到处找线索,如何做到这一点,但MongoDB中没有任何文件的C#API或GridFS的C#。我感到困惑和迷茫,真的可以使用另一套脑筋。

任何一个知道如何真正实现存储被用户上传到一个MongoDB中收集的图像文件上传器?万分感谢!

我已经试过这种变化也没有用。

 数据库DB = mongo.getDB(博客);
GridFile文件=新GridFile(DB);
file.Create(image.jpg的);

VAR图像= db.GetCollection(图像);
images.Insert(file.ToDocument());
 

解决方案

下面的例子展示如何保存文件,并从GridFS的(使用MongoDB的官方驱动程序)回读:

  VAR服务器= MongoServer.Create(MongoDB的://本地主机:27020);
 VAR数据库= server.GetDatabase(tesdb);

 VAR文件名=D:\\ Untitled.png;
 VAR newFileName =D:\\ new_Untitled.png;
 使用(VAR FS =新的FileStream(文件名,FileMode.Open))
 {
    VAR gridFsInfo = database.GridFS.Upload(FS,文件名);
    VAR FILEID = gridFsInfo.Id;

    的ObjectId OID =新的ObjectId(FILEID);
    var文件= database.GridFS.FindOne(Query.EQ(_ ID,OID));

    使用(VAR流= file.OpenRead())
    {
       VAR字节=新字节[stream.Length]
       stream.Read(字节,0,(int)的stream.Length);
       使用(VAR使用newfs =新的FileStream(newFileName,FileMode.Create))
       {
         newFs.Write(字节,0,bytes.Length);
       }
    }
 }
 

结果:

文件:

大块系列:

希望这有助于。

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());

解决方案

Following example show how to save file and read back from gridfs(using official mongodb driver):

 var server = MongoServer.Create("mongodb://localhost:27020");
 var database = server.GetDatabase("tesdb");

 var fileName = "D:\\Untitled.png";
 var newFileName = "D:\\new_Untitled.png";
 using (var fs = new FileStream(fileName, FileMode.Open))
 {
    var gridFsInfo = database.GridFS.Upload(fs, fileName);
    var fileId = gridFsInfo.Id;

    ObjectId oid= new ObjectId(fileId);
    var file = database.GridFS.FindOne(Query.EQ("_id", oid));

    using (var stream = file.OpenRead())
    {
       var bytes = new byte[stream.Length];
       stream.Read(bytes, 0, (int)stream.Length);
       using(var newFs = new FileStream(newFileName, FileMode.Create))
       {
         newFs.Write(bytes, 0, bytes.Length);
       } 
    }
 }

Results:

File:

Chunks collection:

Hope this help.

这篇关于MongoDB的GridFS的用C#,如何存储文件,如图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆