• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#通过操作mongodbgridfs实现文件的数据库存储

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

@(编程)

源码

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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
LeetCodeOnlineJudge题目C#练习-SparialMatrixII发布时间:2022-07-13
下一篇:
C#操纵Excel文件发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap