MongoDB入门(8)- c#通过操作MongoDB GridFS实现文件的数据库存储
GridFS介绍
GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件。
GridFS
GridFS长啥样
/* 1 */
{
"_id" : ObjectId("57302e18d9673e5bf4a4d3c6"),
"filename" : "字段.txt",
"aliases" : null,
"chunkSize" : NumberLong(262144),
"uploadDate" : ISODate("2016-05-09T06:28:40.672Z"),
"length" : NumberLong(1672),
"contentType" : null,
"md5" : "2f85982b0fafcbfe6a3c9387ba49ea3b"
}
/* 2 */
{
"_id" : ObjectId("57305107d96797bb9f96ed9f"),
"filename" : "字段.txt",
"aliases" : null,
"chunkSize" : NumberLong(262144),
"uploadDate" : ISODate("2016-05-09T08:57:43.277Z"),
"length" : NumberLong(1672),
"contentType" : null,
"md5" : "2f85982b0fafcbfe6a3c9387ba49ea3b"
}
/* 3 */
{
"_id" : ObjectId("573051d7d96700d357e6706f"),
"filename" : "字段.txt",
"aliases" : null,
"chunkSize" : NumberLong(262144),
"uploadDate" : ISODate("2016-05-09T09:01:11.060Z"),
"length" : NumberLong(1672),
"contentType" : null,
"md5" : "2f85982b0fafcbfe6a3c9387ba49ea3b"
}
源码
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using System.IO;
namespace Wisdombud.Mongo
{
/// <summary>
///
/// </summary>
public class MongoFileBll
{
private MongoDatabase repository;
/// <summary>
///
/// </summary>
/// <returns></returns>
public MongoCursor<MongoGridFSFileInfo> FindAll()
{
return this.repository.GetGridFS(MongoGridFSSettings.Defaults).FindAll();
}
/// <summary>
///
/// </summary>
/// <param name="pConnectionstring"></param>
public MongoFileBll(string pConnectionstring)
{
MongoUrl mongourl = MongoUrl.Create(pConnectionstring);
var client = new MongoClient(mongourl);
MongoServer server = client.GetServer();
this.repository = server.GetDatabase(mongourl.DatabaseName);
}
/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
public void UploadFile(string filePath)
{
FileInfo fi = new FileInfo(filePath);
this.repository.GetGridFS(MongoGridFSSettings.Defaults).Upload(filePath, fi.Name);
}
/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
public void UploadFile(string filePath, string fileName)
{
this.repository.GetGridFS(MongoGridFSSettings.Defaults).Upload(filePath, fileName);
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
public void DownloadFile(string fileName)
{
this.repository.GetGridFS(MongoGridFSSettings.Defaults).Download(fileName);
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
public void DeleteFile(string fileName)
{
this.repository.GetGridFS(MongoGridFSSettings.Defaults).Delete(fileName);
}
/// <summary>
///
/// </summary>
public void DeleteAll()
{
foreach (var inst in this.repository.GetGridFS(MongoGridFSSettings.Defaults).FindAll())
{
inst.Delete();
}
}
}
}
这个源码持续更新中
用法
MongoFileBll tl = new MongoFileBll("mongodb://127.0.0.1:27017/chzhaotest");
string folder = @"D:\Wisdombud\2016\20160324_Wisdombud.Mongo\SourceCode\Wisdombud.Mongo.Sample\bin\Debug";
DirectoryInfo di = new DirectoryInfo(folder);
foreach (var file in di.GetFiles())
{
tl.UploadFile(file.FullName, Guid.NewGuid().ToString());
}
var aaa = tl.FindAll();
foreach (var inst in aaa)
{
Console.WriteLine(inst.Name);
}