Пример #1
0
        /// <summary>
        /// 下载成流
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public Stream DownloadToStreamByName(string filename)
        {
            Stream destination = new MemoryStream();

            bucket.DownloadToStreamByName(filename, destination);
            return(destination);
        }
        public Stream Download()
        {
            DownloadResponse response = new DownloadResponse()
            {
                FileName = SysConfig.FileName,
                Stream   = new MemoryStream()
            };

            try
            {
                // open DB client and get DB reference
                var client   = new MongoClient(SysConfig.DBconn);
                var database = client.GetDatabase("da_pp_db");

                // download file as stream
                var bucket = new GridFSBucket(database);
                bucket.DownloadToStreamByName(response.FileName, response.Stream);

                // return the stream
                //response.Errored = false;
                //response.Message = "Stream initiated";
                return(response.Stream);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        public void DownloadToStreamByName(string fileName, Stream source, GridFSDownloadByNameOptions options = null)
        {
            MongoClient client = GetClient(m_connectionStr);
            var         db     = client.GetDatabase(DatabaseName);
            var         bucket = new GridFSBucket(db, BucksOptions);

            bucket.DownloadToStreamByName(fileName, source, options);
        }
Пример #4
0
        /// <summary>
        /// 根据文件名下载文件
        /// </summary>
        /// <param name="filepath">保存文件的完整路径</param>
        /// <param name="fileName">文件名(不含扩展名)</param>
        /// <param name="collectionName">mongodb集合名</param>
        public void DownLoadFileByName(string filepath, string fileName, string collectionName)
        {
            FileStream fileStream = new FileStream(filepath, FileMode.Append);
            var        bucket     = new GridFSBucket(this.MongoDatabase, new GridFSBucketOptions
            {
                BucketName     = collectionName,
                ChunkSizeBytes = 358400,
                WriteConcern   = WriteConcern.WMajority,
                ReadPreference = ReadPreference.Secondary
            });
            var options = new GridFSDownloadByNameOptions
            {
                Revision = 0
            };

            bucket.DownloadToStreamByName(fileName, fileStream, options);
            fileStream.Close();
        }