Background
Assuming that using MongoDB's Gridfs to do a distributed file system, the same file is stored in a file system, then the file will need to determine whether the file already exists, in Gridfs each file has a unique MD5 hash value, Only need to use the MD5 value of the file to determine whether in the Gridfs already exist on it, the so-called second-pass function is to use this principle.
Technology stack
C #, VS2013, MongoDB, Gridfs
Realize
1. First get the MD5 value according to the file stream, the code is as follows:
The code is as follows |
Copy Code |
MD5 code for the computed file public static string Getmd5hash (Stream stream) { string result = ""; String hashdata = ""; Byte[] Arrbythashvalue; System.Security.Cryptography.MD5CryptoServiceProvider Md5hasher = New System.Security.Cryptography.MD5CryptoServiceProvider (); Try { Arrbythashvalue = Md5hasher.computehash (stream);//Compute the hash value of the specified Stream object A string of hexadecimal pairs separated by hyphens in which each pair represents the corresponding element in value; for example, "f-2c-4a" Hashdata = System.BitConverter.ToString (Arrbythashvalue); Replace Hashdata = Hashdata.replace ("-", ""); result = Hashdata; } catch (System.Exception ex) { Record log } return result; } |
2. The uniqueness checksum in the Gridfs is based on the MD5 value, as follows:
The code is as follows |
Copy Code |
public string isexist (string tag) { String id = ""; var files = _gridfs.find (Query.eq ("MD5"), Bsonvalue.create (tag)); if (files. Count () > 0) { Id=files. FirstOrDefault (). Id.tostring (); } return ID; } |
3. The complete code reference for the Gridfs action class