@(编程)
源码
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);
}
|
请发表评论