码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-12-11 17:20:13      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:ogr   system   epo   names   sam   filename   方法   using   数据   

 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 }

 

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

标签:ogr   system   epo   names   sam   filename   方法   using   数据   

原文地址:https://www.cnblogs.com/webenh/p/12023503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!