zoukankan      html  css  js  c++  java
  • 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:Wisdombud201620160324_Wisdombud.MongoSourceCodeWisdombud.Mongo.SampleinDebug";
    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);
    }
    
  • 相关阅读:
    Codeforces Round #319 (Div. 2) D
    Codeforces Round #324 (Div. 2) E
    hdu1762 树的上的查询
    GitChat---------顶级高手交流会,好想成为会员哦
    关于项目中遇到的问题-- vue登陆拦截()
    关于项目中遇到的问题--vue格式问题
    项目中遇到的问题------JD商城(相对路径和绝对路径)、以及导出的返回更改
    项目中遇到的问题------vue 配置后台接口方式?
    项目中遇到的问题------vue构建好后IDEA导入VUE项目报错的问题
    项目中遇到的问题------后台已搭建好,怎么构建前端VUE?
  • 原文地址:https://www.cnblogs.com/wardensky/p/5331969.html
Copyright © 2011-2022 走看看