Because the document structure of MongoDB is in the Bjson format (bjson full name: Binary JSON), and the Bjson format itself supports data in binary format, you can save the data in binary format of the file directly into the MongoDB document structure. However, because the maximum length of a bjson cannot exceed 4M, it limits the maximum number of files that can be deposited in a single document beyond 4M. To provide support for bulk file access, the Samus driver provides a "GRIDFS" approach to support, and "Gridfs" mode file operations require the introduction of a new assembly "MongoDB.GridFS.dll". Here are two ways to implement each.
first, Access files in the Document object
When the file size is small, directly into the document object to achieve more concise. For example, a large number of image file access, general picture files will not exceed 4M. We first implement an upload image into the database, and then take out to write back to the page example:
1. Save the picture to Bjson view source print?
02 |
Save the picture in the Bjson |
04 |
public void Saveimgbjson (byte[] byteimg) |
06 |
Document doc = new document (); |
09 |
Mongocollection.save (DOC); |
2. Get the picture byte data stored bjson mode view source print?
2 |
Get picture byte data stored in Bjson mode |
4 |
Public byte[] Getimgbjson () |
6 |
Document Doc= Mongocollection.findone (new document {{"ID", 1}}); |
7 |
return doc["IMG"] as Binary; |
The above two pieces of code are two methods added to the BLL encapsulation class for MONGODB related operations to encapsulate the contents of the previous section. Here's a look at how to call in WebForm:
In the interface, drag out a FileUpload control and a button control, Page CS class Add the following method: View source print?
1 |
protected void Button1_Click (object sender, EventArgs e) |
3 |
IMGBLL IMGBLL = new IMGBLL (); |
5 |
Imgbll.saveimgbjson (fileupload1.filebytes); |
6 |
Response.BinaryWrite (Imgbll.getimgbjson ()); |
second, the use of Gridfs way to access files
Before realizing the Gridfs way, I'll talk about its principle and why I can save large files. The driver first creates two collections in the current database: the "Fs.files" and "Fs.chunks" collections, which record file names, file creation times, file types, and other basic information, which blocks the binary data of the file (and supports encrypting the binary data). Chunking means dividing a file by a specified size and then depositing it into multiple documents. "Fs.files" How to know it corresponds to the file binary data in which blocks. That is because there is a "files_id" key in "Fs.chunks", which corresponds to the "_id" of "fs.files". "Fs.chunks" also has a key (int type) "n", which indicates the order of the blocks. The "FS" in the two collection names can also be customized by parameters.
If you just want to know how to use, you can ignore the above paragraph, the following will use:
1. Gridfs Way of File New, read, delete view source print?
01 |
private string Gridfssave (byte[] bytefile) |
03 |
string filename = Guid.NewGuid (). ToString (); |
05 |
Here the Gridfile constructor has an overload, and the bucket parameter is used to replace the default "FS" in the Create collection name. |
06 |
Gridfile gridfile = new Gridfile (mongodatabase); |
07 |
using (Gridfilestream Gridfilestream = gridfile.create (filename)) |
09 |
Gridfilestream.write (bytefile, 0, bytefile.length); |
14 |
Private byte[] Gridfsread (string filename) |
16 |
Gridfile gridfile = new Gridfile (mongodatabase); |
17 |
Gridfilestream gridfilestream = gridfile.openread (filename); |
18 |
byte[] bytes = new Byte[gridfilestream.length]; |
19 |
Gridfilestream.read (bytes, 0, bytes. Length); |
23 |
private void Gridfsdelete (string filename) |
25 |
Gridfile gridfile = new Gridfile (mongodatabase); |
26 |
Gridfile.delete (New Document ("filename", filename)); |
2. Encapsulates the Gridfs operation again, the new document only stores the file name, equivalent to just a key, the new document can also have other than "filename" other keys. View Source print?
02 |
Save the picture in the Gridfs |
04 |
public void Saveimggridfs (byte[] byteimg) |
06 |
string filename = Gridfssave (byteimg); |
08 |
Document doc = new document (); |
10 |
doc["filename"] = filename; |
11 |
Mongocollection.save (DOC); |
15 |
Get pictures stored in Gridfs mode |
17 |
Public byte[] Getimggridfs () |
19 |
Document doc = Mongocollection.findone (new Document {{"ID", 1}}); |
20 |
string filename = doc["filename"]. ToString (); |
21st |
return gridfsread (filename); |
Third, summary
File access should not be difficult, it is worth noting that when you read binary data from a document in the first way, be sure to convert the type to the "Binary" type, and the system's own key "_id", which is not a string type, is "Oid" type.