WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   WebEnh  阅读(1240)  评论(0编辑  收藏  举报
复制代码
 1 using MongoDB.Driver;
 2 using MongoDB.Driver.GridFS;
 3 using System.IO;
 4 
 5 namespace Wisdombud.Mongo
 6 {
 7     /// <summary>
 8     /// 
 9     /// </summary>
10     public class MongoFileBll
11     {
12         private MongoDatabase repository;
13         /// <summary>
14         /// 
15         /// </summary>
16         /// <returns></returns>
17         public MongoCursor<MongoGridFSFileInfo> FindAll()
18         {
19             return this.repository.GetGridFS(MongoGridFSSettings.Defaults).FindAll();
20         }
21         /// <summary>
22         /// 
23         /// </summary>
24         /// <param name="pConnectionstring"></param>
25         public MongoFileBll(string pConnectionstring)
26         {
27             MongoUrl mongourl = MongoUrl.Create(pConnectionstring);
28             var client = new MongoClient(mongourl);
29             MongoServer server = client.GetServer();
30             this.repository = server.GetDatabase(mongourl.DatabaseName);
31         }
32         /// <summary>
33         /// 
34         /// </summary>
35         /// <param name="filePath"></param>
36         public void UploadFile(string filePath)
37         {
38             FileInfo fi = new FileInfo(filePath);
39             this.repository.GetGridFS(MongoGridFSSettings.Defaults).Upload(filePath, fi.Name);
40         }
41         /// <summary>
42         /// 
43         /// </summary>
44         /// <param name="filePath"></param>
45         /// <param name="fileName"></param>
46 
47         public void UploadFile(string filePath, string fileName)
48         {
49             this.repository.GetGridFS(MongoGridFSSettings.Defaults).Upload(filePath, fileName);
50         }
51 
52         /// <summary>
53         /// 
54         /// </summary>
55         /// <param name="fileName"></param>
56         /// <param name="filePath"></param>
57         public void DownloadFile(string fileName)
58         {
59             this.repository.GetGridFS(MongoGridFSSettings.Defaults).Download(fileName);
60         }
61         /// <summary>
62         /// 
63         /// </summary>
64         /// <param name="fileName"></param>
65         public void DeleteFile(string fileName)
66         {
67             this.repository.GetGridFS(MongoGridFSSettings.Defaults).Delete(fileName);
68         }
69         /// <summary>
70         /// 
71         /// </summary>
72         public void DeleteAll()
73         {
74             foreach (var inst in this.repository.GetGridFS(MongoGridFSSettings.Defaults).FindAll())
75             {
76                 inst.Delete();
77             }
78         }
79     }
80 }
复制代码

使用方法

复制代码
 1 MongoFileBll tl = new MongoFileBll("mongodb://127.0.0.1:27017/chzhaotest");
 2 string folder = @"D:\Wisdombud\2016\20160324_Wisdombud.Mongo\SourceCode\Wisdombud.Mongo.Sample\bin\Debug";
 3 DirectoryInfo di = new DirectoryInfo(folder);
 4 foreach (var file in di.GetFiles())
 5 {
 6     tl.UploadFile(file.FullName, Guid.NewGuid().ToString());
 7 }
 8 var aaa = tl.FindAll();
 9 foreach (var inst in aaa)
10 {
11     Console.WriteLine(inst.Name);
12 }
复制代码

 

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2016-12-11 ASP.NET Core 十种方式扩展你的 Views
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多