一、在文檔對象中存取文件
當文件大小較小的時候,直接存入文檔對象實現起來更簡潔。比如大量圖片文件的存取等,一般圖片文件都不會超過4M。我們先實現一個上傳圖片存入數據庫,再取出來寫回頁面的例子:
1. 把圖片存到BJSON中
1
2
3
4
5
6
7
8
9
10
|
/// <summary>
/// 把圖片存到BJSON中
/// </summary>
public
void SaveImgBJSON( byte [] byteImg)
{ Document doc =
new Document();
doc[ "ID" ] = 1;
doc[ "Img" ] = byteImg;
mongoCollection.Save(doc);
} |
2. 獲取BJSON方式存儲的圖片字節數據
1
2
3
4
5
6
7
8
|
/// <summary>
/// 獲取BJSON方式存儲的圖片字節數據
/// </summary>
public
byte [] GetImgBJSON() { Document doc= mongoCollection.FindOne( new
Document { { "ID" , 1 } });
return
doc[ "Img" ]
as Binary;
} |
上面兩段代碼是在對MongoDB相關操作進行BLL封裝類中添加的兩個方法,封裝方式查看上節內容。下面看看在webform中如何調用:
在界面拖出一個FileUpload控件和一個Button控件,頁面cs類加如下方法:
1
2
3
4
5
6
7
|
protected
void Button1_Click( object
sender, EventArgs e) { ImgBLL imgBll =
new ImgBLL();
imgBll.DeleteAll();
imgBll.SaveImgBJSON(FileUpload1.FileBytes);
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方式的文件新建,讀取,刪除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
private
string GridFsSave( byte [] byteFile)
{ string
filename = Guid.NewGuid().ToString(); //這裏GridFile構造函數有個重載,bucket參數就是用來替換那個創建集合名中默認的"fs"的。
GridFile gridFile =
new GridFile(mongoDatabase);
using
(GridFileStream gridFileStream = gridFile.Create(filename))
{
gridFileStream.Write(byteFile, 0, byteFile.Length);
}
return
filename; } private
byte [] GridFsRead( string
filename) { GridFile gridFile =
new GridFile(mongoDatabase);
GridFileStream gridFileStream = gridFile.OpenRead(filename);
byte [] bytes =
new byte [gridFileStream.Length];
gridFileStream.Read(bytes, 0, bytes.Length);
return
bytes; } private
void GridFsDelete( string
filename) { GridFile gridFile =
new GridFile(mongoDatabase);
gridFile.Delete( new
Document( "filename" , filename));
} |
2. 再次封裝GridFS操作,新文檔只存儲文件名稱,相當於只是一個鍵,新文檔還可以有除“文件名”之外其他的鍵。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/// <summary>
/// 把圖片存到GridFS中
/// </summary>
public
void SaveImgGridFS( byte [] byteImg)
{ string
filename = GridFsSave(byteImg); Document doc =
new Document();
doc[ "ID" ] = 1;
doc[ "filename" ] = filename;
mongoCollection.Save(doc);
} /// <summary>
/// 獲取GridFS方式存儲的圖片
/// </summary>
public
byte [] GetImgGridFS()
{ Document doc = mongoCollection.FindOne( new
Document { { "ID" , 1 } });
string
filename = doc[ "filename" ].ToString();
return
GridFsRead(filename); } |
三、小結
文件存取應該不是很難,值得注意的地方是:用第一種方式從文檔中讀出二進制數據時,一定要將類型轉換爲“Binary”類型;還有系統自帶的鍵“_id”,它也不是string類型,是“Oid”類型的。