Download() public method

Downloads the most recent version of a GridFS file.
public Download ( Stream stream, IMongoQuery query ) : void
stream Stream The destination stream.
query IMongoQuery The GridFS file.
return void
Example #1
0
        /// <summary>
        /// 下载文档到本地
        /// </summary>
        /// <param name="localfile"></param>
        /// <param name="remoteFile"></param>
        public void Copy(string localfile, string remoteFile)
        {
            _logger.DebugFormat("Copy {0} to {1}.", localfile, remoteFile);

            try
            {
                MongoGridFS fs = new MongoGridFS(_context.DataBase);
                fs.Download(localfile, remoteFile);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                throw;
            }
        }
Example #2
0
        static void Test2()
        {
            MongoServer ms = MongoServer.Create();
            string _dbName = "docs";

            MongoDatabase md = ms.GetDatabase(_dbName);
            if (!md.CollectionExists(_dbName))
            {
                md.CreateCollection(_dbName);
            } // End if (!md.CollectionExists(_dbName))

            MongoCollection<Doc> _documents = md.GetCollection<Doc>(_dbName);
            _documents.RemoveAll();
            //add file to GridFS

            MongoGridFS gfs = new MongoGridFS(md);
            MongoGridFSFileInfo gfsi = gfs.Upload(@"c:\mongodb.rtf");
            _documents.Insert(new Doc()
                {
                    DocId = gfsi.Id.AsObjectId,
                    DocName = @"c:\foo.rtf"
                }
            );

            foreach (Doc item in _documents.FindAll())
            {

                ObjectId _documentid = new ObjectId(item.DocId.ToString());
                MongoGridFSFileInfo _fileInfo = md.GridFS.FindOne(Query.EQ("_id", _documentid));
                gfs.Download(item.DocName, _fileInfo);
                System.Console.WriteLine("Downloaded {0}", item.DocName);
                System.Console.WriteLine("DocName {0} dowloaded", item.DocName);
            } // Next item

            //System.Console.ReadKey();
        }