<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>GridFS on 冯威的博客</title><link>https://fwhyy.com/tags/gridfs/</link><description>Recent content in GridFS on 冯威的博客</description><generator>Hugo</generator><language>zh-CN</language><lastBuildDate>Tue, 26 Jan 2016 00:00:00 +0800</lastBuildDate><atom:link href="https://fwhyy.com/tags/gridfs/atom.xml" rel="self" type="application/rss+xml"/><item><title>OWA实现Word在线编辑（以GridFs存储文件）</title><link>https://fwhyy.com/2016/01/word-owa-online-editor-from-gridfs/</link><pubDate>Tue, 26 Jan 2016 00:00:00 +0800</pubDate><guid>https://fwhyy.com/2016/01/word-owa-online-editor-from-gridfs/</guid><description>&lt;p&gt;OWA全称Office Web App，是用来做Office文档预览的一个很好的工具，通过一些配置文件的设置还可以实现Excel、PowerPoint的在线编辑功能，但对Word在线编辑并不支持。GitHub上有大牛实现了Word的在线编辑，不过文件必须是物理文件，本文在此基础上做了些扩展可以支持文件存储在分布式文件系统中，本文以GridFs为例。&lt;/p&gt;</description></item><item><title>C#用md5判断GridFS中文件是否存在</title><link>https://fwhyy.com/2015/05/using-csharp-check-file-in-gridfs/</link><pubDate>Thu, 21 May 2015 00:00:00 +0800</pubDate><guid>https://fwhyy.com/2015/05/using-csharp-check-file-in-gridfs/</guid><description>&lt;p&gt;假设使用MongoDB的GridFS做分布式文件系统，同样的文件在文件系统中只存一份，那么在存入文件时就需要判断文件是否已经存在，在GridFS中每个文件都有唯一的md5哈希值，只需要用文件的md5值判断是否在GridFS中已经存在就可以了，所谓的秒传功能就是用的该原理。&lt;/p&gt;
&lt;h2 id="技术栈"&gt;技术栈&lt;/h2&gt;
&lt;p&gt;C#、VS2013、MongoDB、GridFS&lt;/p&gt;
&lt;h2 id="实现"&gt;实现&lt;/h2&gt;
&lt;p&gt;1 . 首先根据文件流得到md5值，代码如下：&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt; //计算文件的MD5码
 public static string GetMD5Hash(Stream stream)
 {
 string result = &amp;#34;&amp;#34;;
 string hashData = &amp;#34;&amp;#34;;
 byte[] arrbytHashValue;
 System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher =
 new System.Security.Cryptography.MD5CryptoServiceProvider();

 try
 {
 arrbytHashValue = md5Hasher.ComputeHash(stream);//计算指定Stream 对象的哈希值
 //由以连字符分隔的十六进制对构成的String，其中每一对表示value 中对应的元素；例如“F-2C-4A”
 hashData = System.BitConverter.ToString(arrbytHashValue);
 //替换-
 hashData = hashData.Replace(&amp;#34;-&amp;#34;, &amp;#34;&amp;#34;);
 result = hashData;
 }
 catch (System.Exception ex)
 {
 //记录日志
 }

 return result;
 }
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;2 . 根据md5值在GridFS中进行唯一性校验，方法如下：&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt; public string IsExist(string tag)
 {
 string id = &amp;#34;&amp;#34;;
 var files = _gridFs.Find(Query.EQ(&amp;#34;md5&amp;#34;, BsonValue.Create(tag)));
 if(files.Count() &amp;gt; 0)
 {
 id=files.FirstOrDefault().Id.ToString();
 }
 return id;
 }
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;3 . GridFS操作类的完整代码参考&lt;/p&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/oec2003/6c1d473a5a5b61a9d131"&gt;https://gist.github.com/oec2003/6c1d473a5a5b61a9d131&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>