項目總結——MVC+MongoDB實現檔案上傳

來源:互聯網
上載者:User

標籤:

  在做項目的時候我們遇到了視頻上傳的問題。正式開始項目之前做了一個簡單的Demo實現在MVC中視頻檔案的上

傳,考慮到將視頻放到MongoDB中上傳和讀取速度慢的問題,這次我們實現的檔案上傳是儲存的路徑,讀取的額時候

直接通過路徑讀取就OK了。     MVC,M指Model,我目前把它理解成三層中的Entity層,進行資料的傳遞,當然裡邊也可以放一些商務邏輯的代

碼。V,指View層,視圖,用於顯示介面,C指Controller,用於控制介面的顯示。MongoDB是現在非常流行的NoSQL數

據庫,具體的介紹前面有幾篇部落格已經介紹過了,大家可以看一下。     下面看一下代碼實現。     Mongo串連資料庫,跟我們以前串連資料庫的方法一樣,如下:

[csharp] view plaincopy
  1.   

 

[csharp] view plaincopy
  1. public class DBcon  
  2.     {  
  3.         public const string _connectionString = "Server=192.168.24.***:27017";  
  4.   
  5.         public const string _vediotest = "Vediotest";  
  6.     }  

            192.168.24.***是要已連線的服務器的網址,27017是伺服器指定的串連連接埠。本機地址,直接寫連接埠就可以。

 

    接下來是實現向Mongo中添加資料的方法。

 

[csharp] view plaincopy
  1. //上傳視頻  
  2.        public static void AddVedio(VedioTestModels model)  
  3.        {  
  4.            using (Mongo mg = new Mongo(DBcon._connectionString))  
  5.            {  
  6.                mg.Connect();  
  7.                var db = mg.GetDatabase(DBcon._vediotest);  
  8.                var list = db.GetCollection<VedioTestModels>();  
  9.                list.Insert(model);  
  10.            }  
  11.        }  

controler中的方法。

 

 

[csharp] view plaincopy
  1. //向資料庫中存入資訊  
  2.         [AcceptVerbs(HttpVerbs.Post)]  
  3.         public ActionResult Index2(HttpPostedFileBase file, HttpPostedFileBase text,VedioTestModels model)  
  4.          {  
  5.              if (file.ContentLength > 0)  
  6.              {  
  7.                  //獲得儲存路徑  
  8.                  string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),  
  9.                                  Path.GetFileName(file.FileName));  
  10.                  file.SaveAs(filePath);  
  11.   
  12.                  model.vedio = filePath;  
  13.                  model.Id = Guid.NewGuid();  
  14.                  model.vedioName = "../../Uploads/" + Path.GetFileName(file.FileName);  
  15.                  //model.Id=Request["text"];  
  16.                  Biz.BizModel.AddVedio(model);  
  17.             }  
  18.             return View();  
  19.         }  

 

 

    view中是以提交表單的方式實現的,向Controler中傳遞資料。

 

[csharp] view plaincopy
  1. @using (Html.BeginForm("Index2", "VedioTest", FormMethod.Post, new { enctype = "multipart/form-data" }))  
  2. {   
  3. @*<form action="upload" method="post" enctype="multipart/form-data"> *@  
  4.     <form>   
  5.      <input type="file" name="file" /><br />   
  6.      <input type="text" name="text" /><br />  
  7.      <input type="submit" name="Submit" id="Submit"/>   
  8. </form>   
  9. }  

  當然在串連mongo之前要開啟服務,首先開機mongo,其次開啟連接埠。這個可以通過寫批次檔,單擊批處理文

 

件開啟。

    開啟mongo的代碼:mongod --dbpath E:\MongeDBData

    開啟連接埠的代碼:mongo 127.0.0.1:27017/admin

    下面展示一下實現的效果:

    (1)選擇要上傳的檔案

(2)查詢資料庫,資料庫中已經加入上傳資訊

(3)檔案已經上傳到指定檔案加下(Uploads)

 

項目總結——MVC+MongoDB實現檔案上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.