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:

public class Dbcon    {public        const string _connectionstring = "server=192.168.24.***:27017";        Public Const string _vediotest = "Vediotest";    }
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.

Upload video public        static void Addvedio (Vediotestmodels model)        {            using (Mongo mg = new Mongo (dbcon._ connectionString))            {                mg. Connect ();                var db = mg. Getdatabase (dbcon._vediotest);                var list = db. Getcollection<vediotestmodels> ();                List. Insert (model);            }        }
Controlermethods in the.

Depositing information into the database        [Acceptverbs (httpverbs.post)] public        actionresult Index2 (httppostedfilebase file, HttpPostedFileBase text,vediotestmodels model)         {             if (file. ContentLength > 0)             {                 //Get save path                 string filePath = Path.Combine (HttpContext.Server.MapPath (". /uploads "),                                 path.getfilename (file. FileName));                 File. SaveAs (FilePath);                 Model.vedio = FilePath;                 Model. Id = Guid.NewGuid ();                 Model.vedioname = ". /.. /uploads/"+ path.getfilename (file. FileName);                 Model. id=request["text"];                 Biz.BizModel.AddVedio (model);            }            return View ();        }


View is implemented as a form submission, passing data to the controler.

@using (Html.BeginForm ("Index2", "Vediotest", FormMethod.Post, new {enctype = "multipart/form-data"})) {@*<form acti on= "Upload" method= "post" enctype= "Multipart/form-data" > *@    <form>      <input type= "file" Name= " File "/><br/>      <input type=" text "name=" text "/><br/> <input     type=" Submit "Name=" Submit "id=" Submit "/> </form>}"
of courseto turn on the service before connecting MONGO, start the MONGO first, and then open 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.