public string InsertFileByPath(string set_name, string file_path, Dictionary <object, object> info = null) { MongoGridFSSettings mgfs = new MongoGridFSSettings(); mgfs.Root = set_name; MongoGridFSCreateOptions mgfsco = new MongoGridFSCreateOptions(); if (info == null) { info = new Dictionary <object, object>(); } FileInfo fi = new FileInfo(file_path); info.Add("file_name", fi.Name); info.Add("timestamp", GetTimestamp()); info.Add("content_type", fi.Extension); mgfsco.Metadata = new BsonDocument(info); if (fi.Exists) { using (FileStream fs = new FileStream(file_path, FileMode.Open)) { MongoGridFSFileInfo file_info = mdbs.GetGridFS(mgfs).Upload(fs, fi.Name, mgfsco); if (file_info.Exists) { return(file_info.Id.ToString()); } } } return(null); }
public List <string> InsertFilesByPath(string set_name, string directory, Dictionary <object, object> infos = null) { MongoGridFSSettings mgfs = new MongoGridFSSettings(); mgfs.Root = set_name; List <string> ids = new List <string>(); DirectoryInfo dirInfo = new DirectoryInfo(directory); if (dirInfo.Exists) { foreach (FileInfo file in dirInfo.GetFiles()) { MongoGridFSCreateOptions mgfsco = new MongoGridFSCreateOptions(); Dictionary <object, object> info = new Dictionary <object, object>(); info.Add("file_name", file.Name); info.Add("timestamp", GetTimestamp()); info.Add("content_type", file.Extension); if (infos != null) { info.ToList().ForEach(item => info.Add(item.Key, item.Value)); } mgfsco.Metadata = new BsonDocument(info); using (FileStream fs = new FileStream(file.FullName, FileMode.Open)) { MongoGridFSFileInfo file_info = mdbs.GetGridFS(mgfs).Upload(fs, file.Name, mgfsco); if (file_info.Exists) { ids.Add(file_info.Id.ToString()); } } } } return(ids); }
public void TestRemoveAllGFSandDelete() { var server = MongoServer.Create("mongodb://localhost:27017"); var db = server.GetDatabase("dcm"); var gridSettings = new MongoGridFSSettings(); MongoGridFS fs = db.GetGridFS(gridSettings); fs.Files.RemoveAll(); }