Because MongoDB's document structure is in bjson format (bjson Full name: Binary JSON), bjson itself supports storing binary data, therefore, the binary data of the file can be directly stored in the document structure of MongoDB. However, because the maximum length of a bjson file cannot exceed 4 MB, the maximum size of files that can be stored in a single document cannot exceed 4 MB. To provide support for large-capacity file access, the samus Driver provides the "gridfs" method. The "gridfs" method requires the introduction of new Program Set "MongoDB. gridfs. dll ". The following two methods are used for implementation.
1. access a file in a Document Object
When the file size is small, it is simpler to directly store the file object. For example, the access to a large number of image files is generally not more than 4 MB. The following example shows how to store an uploaded image to a database and write it back to the page:
1. Save the image to bjson
02 |
/// Save the image to bjson |
04 |
Public Void Saveimgbjson ( Byte [] Byteimg) |
06 |
Document Doc = New Document (); |
08 |
Doc [ "IMG" ] = Byteimg; |
09 |
Collections collection. Save (DOC ); |
2. Obtain the byte data of images stored in bjson format
2 |
/// Obtain the byte data of the image stored in bjson format |
4 |
Public Byte [] Getimgbjson () |
6 |
Document Doc = collections collection. findone ( New Document {{ "ID" , 1 }}); |
7 |
Return Doc [ "IMG" ] As Binary; |
The above two paragraphsCodeThis is the two methods added to the BLL encapsulation class for MongoDB-related operations. You can view the content of the previous section in the encapsulation mode. The following describes how to call webform:
Drag a fileupload control and a button control on the interface. Add the following method to the CS class on the page:
1 |
Protected Void Button#click ( Object Sender, eventargs E) |
5 |
Imgbll. saveimgbjson (fileupload1.filebytes ); |
6 |
Response. binarywrite (imgbll. getimgbjson ()); |
2. Access files through gridfs
Before implementing the gridfs method, let's talk about its principle and why it can store large files. The driver first creates two sets in the current database: "FS. files "and" FS. chunks "set. The former records basic information such as the file name, File Creation Time, and file type. The latter stores binary data of the file in blocks (and supports encryption of these binary data ). Block splitting means to split the file according to the specified size and store it in multiple documents. "Fs. Files": How do you know the binary data of the corresponding file? That's because there is a "files_id" key in "fs. chunks", which corresponds to "_ id" of "fs. Files ". "Fs. chunks" also has a key (INT type) "N", which indicates the order of these blocks. The "FS" in the two collection names can also be customized by parameters.
If you just want to know how to use it, you can ignore the above section and use it as follows:
1. Create, read, and delete gridfs files
01 |
Private String Gridfssave ( Byte [] Bytefile) |
03 |
String Filename = guid. newguid (). tostring (); |
05 |
// Here, the gridfile constructor has an overload. The bucket parameter is used to replace the default "FS" in the created set name. |
06 |
Gridfile = New Gridfile (relational database ); |
07 |
Using (Gridfilestream = gridfile. Create (filename )) |
09 |
Gridfilestream. Write (bytefile, 0, bytefile. Length ); |
14 |
Private Byte [] Gridfsread ( String Filename) |
16 |
Gridfile = New Gridfile (relational database ); |
17 |
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 = New Gridfile (relational database ); |
26 |
Gridfile. Delete ( New Document ( "FILENAME" , Filename )); |
2. Re-encapsulate the gridfs operation. The new file only stores the file name, which is equivalent to a key. The new file can also have a key other than the "file name.
02 |
/// Save the image to gridfs |
04 |
Public Void Saveimggridfs ( Byte [] Byteimg) |
06 |
String Filename = gridfssave (byteimg ); |
08 |
Document Doc = New Document (); |
10 |
Doc [ "FILENAME" ] = Filename; |
11 |
Collections collection. Save (DOC ); |
15 |
/// Obtain images stored in gridfs Mode |
17 |
Public Byte [] Getimggridfs () |
19 |
Document Doc = collections collection. findone ( New Document {{ "ID" , 1 }}); |
20 |
String Filename = Doc [ "FILENAME" ]. Tostring (); |
21 |
Return Gridfsread (filename ); |
Iii. Summary
File access should not be difficult. It is worth noting that when reading binary data from the document in the first way, the type must be converted to the "binary" type; there is also the built-in key "_ id", which is not of the string type and is of the "oid" type.
Source: http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html