public void TestReadGridFS2Array() { var server = MongoServer.Create("mongodb://localhost:27017"); var db = server.GetDatabase("dcm"); var where = Query.EQ("md5", "cac1807172774be5d48937cee90c9e84"); MongoGridFS fs = db.GridFS; MongoGridFSFileInfo fileInfo = fs.FindOne(where); MongoGridFSStream readerFS = fileInfo.Open(System.IO.FileMode.Open); List <byte> readByte = new List <byte>(); int bufferSize = 4048; byte[] buffer = new byte[bufferSize]; int readCount = readerFS.Read(buffer, 0, bufferSize); while (readCount > 0) { readByte.AddRange(buffer.Take(readCount).ToArray()); readCount = readerFS.Read(buffer, 0, bufferSize); } }
/// <summary> /// 获取流 /// </summary> /// <param name="remoteFile"></param> /// <returns></returns> public byte[] GetBytes(string remoteFile) { _logger.DebugFormat("Get Stream by Id {0}", remoteFile); try { MongoGridFS fs = new MongoGridFS(_context.DataBase); byte[] bytes; using (MongoGridFSStream gfs = fs.OpenRead(remoteFile)) { bytes = new Byte[gfs.Length]; gfs.Read(bytes, 0, bytes.Length); } return(bytes); } catch (Exception ex) { _logger.Error(ex.Message); _logger.Error(ex.StackTrace); throw; } }