public void TestCreateZeroLengthFile()
        {
            gridFS.Files.RemoveAll();
            gridFS.Chunks.RemoveAll();
            gridFS.Chunks.ResetIndexCache();

            var fileInfo = gridFS.FindOne("test");

            Assert.IsNull(fileInfo);

            using (var stream = gridFS.Create("test")) {
                Assert.IsTrue(stream.CanRead);
                Assert.IsTrue(stream.CanSeek);
                Assert.IsFalse(stream.CanTimeout);
                Assert.IsTrue(stream.CanWrite);
                Assert.AreEqual(0, stream.Length);
                Assert.AreEqual(0, stream.Position);

                fileInfo = gridFS.FindOne("test");
                Assert.IsTrue(fileInfo.Exists);
                Assert.IsNull(fileInfo.Aliases);
                Assert.AreEqual("test", fileInfo.Name);
                Assert.AreEqual(gridFS.Settings.ChunkSize, fileInfo.ChunkSize);
                Assert.IsNull(fileInfo.ContentType);
                Assert.AreEqual(0, fileInfo.Length);
                Assert.IsNull(fileInfo.MD5);
                Assert.IsNull(fileInfo.Metadata);
            }

            fileInfo = gridFS.FindOne("test");
            Assert.IsTrue(fileInfo.Exists);
            Assert.AreEqual(0, fileInfo.Length);
            Assert.IsNotNull(fileInfo.MD5);
        }
Exemple #2
0
        /// <summary>
        /// 字节数组方式保存文件
        /// </summary>
        /// <param name="fileData">数据内容</param>
        /// <param name="fileName">文件名</param>
        /// <param name="fileCollectionName"></param>
        public virtual void SaveFileByByteArray(byte[] fileData, string fileName, string fileCollectionName)
        {
            try
            {
                if (0 == fileData.Length || string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(fileCollectionName))
                {
                    throw new KnownException("缺失必需数据,无法保存数据!");
                }
                MongoGridFSSettings fsSetting = new MongoGridFSSettings()
                {
                    Root = fileCollectionName
                };

                //MongoGridFS fs = new MongoGridFS(_database, fsSetting);   //  将被遗弃的方法
                MongoGridFS fs = new MongoGridFS(_server, _database.Name, fsSetting);
                MongoGridFSCreateOptions option = new MongoGridFSCreateOptions {
                    UploadDate = DateTime.Now
                };
                using (MongoGridFSStream gfs = fs.Create(fileName, option))
                {
                    gfs.Write(fileData, 0, fileData.Length);
                    gfs.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //  设定查询集合的名称
        }
Exemple #3
0
        /// <summary>
        /// 保存报表结果
        /// </summary>
        /// <param name="savsString"></param>
        /// <param name="rootName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public void SaveFileToMongoDb(string fileName, string savsString, string rootName)
        {
            //BsonDocument doc = new BsonDocument();
            //doc.Add("UserID", 1L);
            //option.Metadata = doc;
            MongoGridFS gf = GetGridFs(Dbname, rootName);

            using (MongoGridFSStream gfs = gf.Create(fileName, new MongoGridFSCreateOptions()
            {
                UploadDate = DateTime.Now
            }))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(savsString);
                gfs.Write(bytes, 0, bytes.Length);
            }
        }