惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy & Cybersecurity Law Blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
Know Your Adversary
Know Your Adversary
F
Fortinet All Blogs
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
Jina AI
Jina AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
T
Tor Project blog
S
Security Affairs
S
Securelist
F
Full Disclosure
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
Apple Machine Learning Research
Apple Machine Learning Research
N
News and Events Feed by Topic
Recorded Future
Recorded Future
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
Stack Overflow Blog
Stack Overflow Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
I
Intezer
Security Latest
Security Latest
Scott Helme
Scott Helme
U
Unit 42
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog

博客园 - goooto

Spring4.1新特性——Spring缓存框架增强(转) Spring整合Ehcache管理缓存(转) 《权力的游戏》第七季片场照曝光 MINA2 框架详解(转) Docker简明教程(转) IIS7.5 Gzip压缩配置 UML 依赖 关联 聚合 组合 亲属称谓 以字节流上传文件 C#命名规范(微软官方版) MongoDB学习笔记(六) MongoDB索引用法和效率分析(转) MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系(转) MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据(转) MongoDB学习笔记(二) 通过samus驱动实现基本数据操作(转) MongoDB学习笔记(一)MongoDB介绍及安装(转) Log4net配置相关 VS2010快捷键大全 JQuery中得到Element真实top、left、height和width属性值的对象 - goooto - 博客园 WindowsService注册
MongoDB学习笔记(五) MongoDB文件存取操作(转)
goooto · 2011-05-05 · via 博客园 - goooto

由于MongoDB的文档结构为BJSON格式(BJSON全称:Binary JSON),而BJSON格式本身就支持保存二进制格式的数据,因此可以把文件的二进制格式的数据直接保存到MongoDB的文档结构中。但是由于一个 BJSON的最大长度不能超过4M,所以限制了单个文档中能存入的最大文件不能超过4M。为了提供对大容量文件存取的支持,samus驱动提供了 “GridFS”方式来支持,“GridFS”方式文件操作需要引入新的程序集“MongoDB.GridFS.dll”。下面我们分别用两种方式来实 现。

一、在文档对象中存取文件

  当文件大小较小的时候,直接存入文档对象实现起来更简洁。比如大量图片文件的存取等,一般图片文件都不会超过4M。我们先实现一个上传图片存入数据库,再取出来写回页面的例子:

   1. 把图片存到BJSON中

04public void SaveImgBJSON(byte[] byteImg)
06    Document doc = new Document();
08    doc["Img"] = byteImg;
09    mongoCollection.Save(doc);

   2. 获取BJSON方式存储的图片字节数据

4public byte[] GetImgBJSON()
6  Document doc=  mongoCollection.FindOne(new Document { { "ID", 1 } });
7  return doc["Img"] as Binary;

  上面两段代码是在对MongoDB相关操作进行BLL封装类中添加的两个方法,封装方式查看上节内容。下面看看在webform中如何调用:

  在界面拖出一个FileUpload控件和一个Button控件,页面cs类加如下方法:

1protected void Button1_Click(object sender, EventArgs e)
3    ImgBLL imgBll = new ImgBLL();
5    imgBll.SaveImgBJSON(FileUpload1.FileBytes);
6    Response.BinaryWrite(imgBll.GetImgBJSON());

二、用GridFS方式存取文件

  在实现GridFS方式前我先讲讲它的原理,为什么可以存大文件。驱动首先会在当前数据库创建两个集合:"fs.files" 和"fs.chunks"集合,前者记录了文件名,文件创建时间,文件类型等基本信息;后者分块存储了文件的二进制数据(并支持加密这些二进制数据)。分 块的意思是把文件按照指定大小分割,然后存入多个文档中。"fs.files"怎么知道它对应的文件二进制数据在哪些块呢?那是因为 在"fs.chunks"中有个"files_id"键,它对应"fs.files"的"_id"。"fs.chunks"还有一个键(int 型)"n",它表明这些块的先后顺序。这两个集合名中的"fs"也是可以通过参数自定义的。

  如果你只是想知道怎么用,可以忽略上面这段话,下面将用法:

   1. GridFS方式的文件新建,读取,删除

01private string GridFsSave(byte[] byteFile)
03    string filename = Guid.NewGuid().ToString();
05    //这里GridFile构造函数有个重载,bucket参数就是用来替换那个创建集合名中默认的"fs"的。
06    GridFile gridFile = new GridFile(mongoDatabase);
07    using (GridFileStream gridFileStream = gridFile.Create(filename))
09        gridFileStream.Write(byteFile, 0, byteFile.Length);
14private byte[] GridFsRead(string filename)
16    GridFile gridFile = new GridFile(mongoDatabase);
17    GridFileStream gridFileStream = gridFile.OpenRead(filename);
18    byte[] bytes = new byte[gridFileStream.Length];
19    gridFileStream.Read(bytes, 0, bytes.Length);
23private void GridFsDelete(string filename)
25    GridFile gridFile = new GridFile(mongoDatabase);
26    gridFile.Delete(new Document("filename", filename));

   2. 再次封装GridFS操作,新文档只存储文件名称,相当于只是一个键,新文档还可以有除“文件名”之外其他的键。

04public void SaveImgGridFS(byte[] byteImg)
06    string filename = GridFsSave(byteImg);
08    Document doc = new Document();
10    doc["filename"] = filename;
11    mongoCollection.Save(doc);
17public byte[] GetImgGridFS()
19    Document doc = mongoCollection.FindOne(new Document { { "ID", 1 } });
20    string filename = doc["filename"].ToString();
21    return GridFsRead(filename);

三、小结

  文件存取应该不是很难,值得注意的地方是:用第一种方式从文档中读出二进制数据时,一定要将类型转换为“Binary”类型;还有系统自带的键“_id”,它也不是string类型,是“Oid”类型的。

原文出处:

http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html