Project summary--mvc+mongodb implement file upload

Source: Internet
Author: User
Tags processing text

When we were doing the project, we encountered the problem of uploading video. Before formally starting the project, a simple demo implementation was made on the video file in MVC

Upload and read slowly in MongoDB, this time we implement the file upload is the path of storage, read the amount of time

It is OK to read directly through the path. Mvc,m refers to model, I currently understand it as the three layer of the entity layer, the data transfer, of course, can also put some business logic generation

Code. V, refers to the view layer, views, for the display interface, C refers to the controller, to control the display of the interface. MongoDB is now a very popular NoSQL number

According to the library, the specific introduction of a few previous blog has been introduced, we can look at.     Let's take a look at the code implementation. MONGO connect to the database the same way we used to connect to the database, as follows:

[CSharp]View Plaincopy

[CSharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. public  class dbcon  
    2. &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;
    3. Li class= "alt focusme" >         public const string _connectionstring =  "server=192.168.24.***:27017";   
    4. &NBSP;&NBSP;
    5.         public < span class= "keyword" >const string _vediotest =  " Vediotest ";   
    6.     }   

192.168.24.*** is the URL of the server you want to connect to, and 27017 is the server-specified connection port. Native address, direct write port is available.

The next step is to implement the method of adding data to the MONGO.

[CSharp]View Plaincopy
  1. Upload Video
  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. }

the method in Controler.

[CSharp]View Plaincopy
  1. Depositing information into the database
  2. [Acceptverbs (Httpverbs.post)]
  3. Public actionresult Index2 (httppostedfilebase file, httppostedfilebase text,vediotestmodels model)
  4. {
  5. if (file. ContentLength > 0)
  6. {
  7. //Get Save path
  8. string filePath = Path.Combine (HttpContext.Server.MapPath (". /uploads "),
  9. Path.getfilename (file. FileName));
  10. File. SaveAs (FilePath);
  11. Model.vedio = FilePath;
  12. Model. Id = Guid.NewGuid ();
  13. Model.vedioname = ". /.. /uploads/"+ path.getfilename (file.  FileName);
  14. //model.  id=request["text"];
  15. Biz.BizModel.AddVedio (model);
  16. }
  17. return View ();
  18. }

View is implemented as a form submission, passing data to the 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. }

of course, before connecting the MONGO to open the service, first boot MONGO, followed by the port. This can be done by writing a batch file, clicking on the batch processing text

Open the piece.

Code to open MONGO: Mongod--dbpath E:\MongeDBData

Code to open port: MONGO 127.0.0.1:27017/admin

The following shows the effect of the implementation:

(1) Select the file to upload

(2) Query the database, the database has been added to upload information

(3) The file has been uploaded to the specified file plus (uploads)

Project summary--mvc+mongodb implement file upload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.