1. 程式人生 > >NoSql-MongoDB GridFS+ASP.NET MVC實現上傳,顯示

NoSql-MongoDB GridFS+ASP.NET MVC實現上傳,顯示

namespace MongoDBTest.Controllers
{
    public class MongoDBHelperController : Controller
    {
        private static MongoDatabase DB;
        public static string fileTable = "files";
        /// <summary>
        /// 連線資料庫設定
        /// </summary>
        public void init()
        {
            //使用AppSettings方式和配置檔案連線,靈活控制MongoDb資料庫位置
            string ConnectionString = ConfigurationManager.AppSettings["mondoDbConnection"];
            //連線本地的資料庫
            //string ConnectionString = "127.0.0.1";
            //連線不成功,提示
            if (String.IsNullOrEmpty(ConnectionString))
            {
                throw new ArgumentNullException("Connection string not found");
            }
            //建立連線池
            MongoServerSettings mongoSetting = new MongoServerSettings();
            mongoSetting.MaxConnectionPoolSize = 15000;//設定最大連線池
            mongoSetting.WaitQueueSize = 500;//設定等待佇列數
            mongoSetting.Server = new MongoServerAddress(ConnectionString, 27017);
            int count = MongoServer.MaxServerCount;
            MongoServer server = MongoServer.Create(mongoSetting);//建立連線資料檔案

            DB = server.GetDatabase("DB3");//建立資料庫連線,連線的字串名稱
        }

        public void ProcessRequest()
        {
            init();
            //從MVC傳值,獲取
            string action = Request.QueryString["actions"];
            //通過action值來判斷是上傳,還是獲取,還是下載等
            switch (action)
            {
                case "DOWNLOAD": DownFile(); break; //下載檔案
                case "UPLOAD": Upload(); break; //上傳檔案
            }
        }

        //上傳檔案
        public void Upload()
        {
            try
            {
                HttpPostedFileBase file = (HttpPostedFileBase)Request.Files["file"];
                //獲取上傳檔案的長度
                int nFileLen = file.ContentLength;
                //獲取上傳檔案的值
                string nFileName = file.FileName;
                //利用GridFS 建立
                MongoGridFSSettings fsSetting = new MongoGridFSSettings() { Root = fileTable };
                MongoGridFS fs = new MongoGridFS(DB, fsSetting);

                byte[] myData = new Byte[nFileLen];
                file.InputStream.Read(myData, 0, nFileLen);
                //呼叫Write、WriteByte、WriteLine函式時需要手動設定上傳時間
                //通過Metadata 新增附加資訊
                MongoGridFSCreateOptions option = new MongoGridFSCreateOptions();
                option.UploadDate = DateTime.Now;
                //建立檔案,檔案並存儲資料
                using (MongoGridFSStream gfs = fs.Create(file.FileName, option))
                {
                    gfs.Write(myData, 0, nFileLen);
                    gfs.Close();
                    Response.Write("恭喜您" + nFileName + "檔案上傳成功!");
                }
            }
            catch (Exception e)
            {
                Response.Write("Sorry your file is not upload successfully!" + e.Message);
            }

            Response.End();
        }
        /// <summary>
        /// 下載檔案方法
        /// </summary>
        public void DownFile()
        {
            //獲取檔案值
            string filename = Request.QueryString["value"];
            //獲取檔案型別
            Response.ContentType = "application/octet-stream";
            //實現下載+檔名
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            //獲取圖片名
            MongoGridFSSettings fsSetting = new MongoGridFSSettings() { Root = fileTable };
            //通過檔名去資料庫查值
            MongoGridFS fs = new MongoGridFS(DB, fsSetting);
            MongoGridFSFileInfo gfInfo = new MongoGridFSFileInfo(fs, filename);
            //方法一,很簡潔
            fs.Download(Response.OutputStream, filename);
            Response.End();
        }
    }
}

MongoDbTest(View):上傳介面顯示