c#通过操作mongodb gridfs实现文件的数据库存储详解编程语言

@(编程)

源码

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

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20954.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论