MongoDB file (MongoDB FS)

Source: Internet
Author: User
Tags findone mongodb server

1. Whether it is storing normal data or storing files, we must first visit the MongoDB database

 Public Static mongodatabase getdatabase ()        {            //MongoDB server address         var New mongoclient (Config.BasicConfig.DbAddress);             var server = client. Getserver ();             // MongoDb database name         return server. Getdatabase (Config.BasicConfig.DbName);        }
View Code

2: Get FS

MongoDB get an instance of FS I know there are two ways to do this.

    • 1: Directly through the Database property call, if we understand MongoDB as SQL Server database, then this way is to get to the default table "FS" instance.
    • 2: By setting the root property value by Mongogridfssettings, MongoDB will generate a specific table based on the root value, without having to create

Don't talk nonsense, look at the code

// accesses a collection or table of files named Test, and if not, automatically creates Dbhelper.getdatabase (). Getgridfs (new"Test"}); // Gets the default collection or table "FS" Dbhelper.getdatabase (). Gridfs;
View Code

3: Implement File Upload
File Upload, MongoDB provides a mongogridfscreateoptions class to improve our file information, I generally only use the inside of the ContentType

// Huo de Wen Jian ji he var fs = Dbhelper.getdatabase (). Getgridfs (new mongogridfssettings {Root = fileType}); // Chuang Jian Wen Jian shu xing, zhi jie shang chuan de wen Jian mei u ContentType Shu Xing, Xu Yao Shou dong zhi di ngvarnew MongoDB.Driver.GridFS.MongoGridFSCreateOptions {ContentType = ContentType}; // Wen Jian Shang Chuan var fileInfo = fs. Upload (stream, filename, op);
View Code

4: File acquisition
When I use MongoDB FS, I use the program to control the file name unique, get the code

//Get File PublicFileitem GetFile (stringFileno,stringFileType) {            //assembly file name            varfilename =string. Format ("{0}_{1}", FileType, Fileno); varFS = Dbhelper.getdatabase (). Getgridfs (Newmongogridfssettings {Root=FileType}); //find a file based on the file name            varFileInfo =FS.            FindOne (filename); if(FileInfo! =NULL&&fileinfo.exists) {using(varFileStream =Fileinfo.open (FileMode.Open, FileAccess.Read)) {                    varbytes =New byte[Filestream.length]; //read file as bytesFileStream.Read (Bytes,0, Bytes.                    Length); return NewFileitem {ContentType= fileinfo.contenttype, filecontent = bytes, Filename = fileinfo.name, MD5 = fileinfo.md5, Size = Fileinfo.length +"", Uploaddate=Fileinfo.uploaddate}; }            }            return NULL; }
View Code

5: File Modification
File modification I'm not talking nonsense, just look at the code.

//modify File (overwrite) PublicFileitem Upload (stringFileno,stringFileType, Stream stream) {            varfilename =string. Format ("{0}_{1}", FileType, Fileno); varFS = Dbhelper.getdatabase (). Getgridfs (Newmongogridfssettings {Root =FileType}); varOldfileinfo =FS.            FindOne (filename); if(Oldfileinfo! =NULL&&oldfileinfo.exists) {varbytes =New byte[Stream.                Length]; Stream. Read (Bytes,0, Bytes.                Length); using(varSW =Oldfileinfo.openwrite ()) {SW. Seek (0, Seekorigin.begin); Sw. Write (Bytes,0, Bytes.                    Length); Sw.                    Flush ();                Oldfileinfo.refresh (); }              }            return NewFileitem {ContentType = oldfileinfo.contenttype, filecontent =NULL, Filename = oldfileinfo.name, MD5 = oldfileinfo.md5, Size = Oldfileinfo.length +"", uploaddate =Oldfileinfo.uploaddate}; }
View Code

Writing good is not important, it is important to provide a reference to those who need, but also hope that the great God to add a few words, so that people more convenient.

MongoDB file (MongoDB FS)

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.