MobileFileQuery.CS(数据层)
/// <summary>
/// 获取指定文件夹下所有的文件名,文件地址,及文件的MD5
/// </summary>
/// <param name="path">文件夹地址</param>
/// <returns></returns>
public List<FolderFile> ListTreeShow(string path)//递归目录 文件
{
//实例化文件夹
DirectoryInfo theDir = new DirectoryInfo(path);
//循环到 文件夹下所有文件的 文件名,文件地址,及文件的MD5
List<FolderFile> dt = GetFiles(theDir.GetFiles());
//获取到文件夹下所有文件夹集合
//DirectoryInfo[] subDirectories = theDir.GetDirectories();//获得目录
//循环文件夹下的所有文件夹
//foreach (DirectoryInfo dirinfo in subDirectories)
//{
// //执行递归 自己掉自己
// dt = ListTreeShow(dirinfo.FullName);
//}
return dt;
}
/// <summary>
/// 根据文件夹获取到文件夹下的所有文件 文件名,文件地址,及文件的MD5
/// </summary>
/// <param name="files">文件信息集合</param>
/// <param name="dt">数据源模型</param>
/// <returns></returns>
private List<FolderFile> GetFiles(FileInfo[] files)
{
List<FolderFile> dt = new List<FolderFile>();
//循环文件信息
foreach (FileInfo file in files)
{
FolderFile dr = new FolderFile();
//文件所属文件夹
dr.FileUrl = file.Directory.ToString();
//文件名称
dr.FileName = file.Name;
//获取文件的MD5值
dr.FileMD5 = GetFileMD5(file.DirectoryName + "\\" + file.Name);
dt.Add(dr);
}
return dt;
}
/// <summary>
/// 根据传入的文件地址获取到 文件的MD5值
/// </summary>
/// <param name="path">文件地址</param>
/// <returns></returns>
private string GetFileMD5(string path)
{
try
{
MD5 md5 = MD5.Create();
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] hash = md5.ComputeHash(fs);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 根据文件的Stream获取文件MD5值
/// </summary>
/// <param name="fileStream"></param>
/// <returns></returns>
public string GetFileMD5ByStream(Stream fileStream)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fileStream);
fileStream.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
string fileMD5 = sb.ToString();
return fileMD5;
}
MobileFileServiceAdapter.CS(逻辑层)
/// <summary>
/// 判断上传文件是否已在文件夹存在(存在返回已存在文件相关信息,未存在返回上传文件的MD5值)
/// </summary>
/// <param name="fileStream">文件Stream</param>
/// <param name="FolderPath">文件夹绝对路径</param>
/// <returns></returns>
public FolderFile IsExists(Stream fileStream, string FolderPath)
{
//上传文件文件MD5
string fileMD5 = _mobileFileQuery.GetFileMD5ByStream(fileStream);
//文件夹内文件集
List<FolderFile> FilesList = _mobileFileQuery.ListTreeShow(FolderPath);
FolderFile file = FilesList.Where(a => a.FileMD5.Equals(fileMD5)).FirstOrDefault();
if (file == null)
{
file = new FolderFile() { FileMD5 = fileMD5 };
}
return file;
}
控制器调用(部分代码)
var files = Request.Files;
if (files == null) return Json("Error", JsonRequestBehavior.AllowGet);
var uploadfile = files[0];
Stream filesMD5 = uploadfile.InputStream;
String FolderPath=” C:/xx/xx”;
FolderFile filesMD5= _mobileFileServiceAdapter. IsExists(filesMD5,FolderPath);
Entity
public class FolderFile
{
/// <summary>
/// 文件所属文件夹
/// </summary>
public string FileUrl { get; set; }
/// <summary>
/// 文件名称
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 文件的MD5值
/// </summary>
public string FileMD5 { get; set; }
}