日期:2014-05-20 浏览次数:20439 次
/// <summary> /// 提供上传文件时所使用的方法 /// </summary> public interface IUploadFile { /// <summary> /// 验证上传文件的扩展名 /// </summary> /// <param name="postFile">System.Web.HttpPostedFile对象</param> /// <returns></returns> bool CheckFileType(System.Web.HttpPostedFile postFile); /// <summary> /// 获取文件名称 /// 调用此方法并不创建新目录 /// </summary> /// <param name="filePath">服务器的虚拟路径</param> /// <param name="fileName">文件名</param> /// <returns></returns> string GetFileName(string filePath,string fileName); } /// <summary> /// 提供上传文件时所使用的方法 /// </summary> public abstract class UploadFile { /// <summary> /// 获取文件路径 /// </summary> /// <param name="path">虚拟路径</param> /// <returns></returns> protected string GetDirectory(string path) { string directoryName = DateTime.Now.ToString("yyyyMMdd"); string filePath = Path.Combine(path, directoryName); if (!Directory.Exists(filePath)) { return Path.Combine(filePath, GetNumber(1)); } return Path.Combine(filePath, GetPath(Directory.GetDirectories(filePath), filePath)); } /// <summary> /// 获取文件夹中的文个数件少于1000的文件夹名称 /// </summary> /// <param name="info">目录名称</param> /// <param name="filePath">路径</param> /// <returns></returns> protected string GetPath(IList<string> info, string filePath) { string fileName = string.Empty; if (info == null) { return null; } if (info.Count <= 0) { return GetNumber(1); } for (int i = 0; i < info.Count; i++) { if (Directory.Exists(info[i])) { if (Directory.GetFiles(info[i]).Length < 1000) { fileName = info[i]; break; } } } if (string.IsNullOrEmpty(fileName)) { return Path.Combine(filePath, GetNumber(int.Parse(info.ToList<string>().Max(s => s.Substring(s.LastIndexOf("\\") + 1).TrimStart('0'))) + 1)); } else { return fileName; } } /// <summary> /// 获取数字字符串,不足4位用0补足 /// </summary> /// <param name="number">不大于9999的数字</param> /// <returns></returns> protected string GetNumber(int number) { if (number > 9999) return null; return (10000 + number).ToString().Substring(1); } #region IUploadFile 成员 /// <summary> /// 验证上传文件的扩展名 /// </summary> /// <param name="postFile">System.Web.HttpPostedFile对象</param> /// <returns></returns> public abstract bool CheckFileType(System.Web.HttpPostedFile postFile); /// <summary> /// 获取文件名称 /// </summary> /// <param name="filePath">服务器的虚拟路径</param> /// <param name="fileName">文件名</param> /// <returns></returns> public virtual string GetFileName(string filePath,string fileName) { DateTime now = DateTime.Now; string name = (100 + now.Hour).ToString().Substring(1) + (100 + now.Minute).ToString().Substring(1) + (100 + now.Second).ToString().Substring(1) + "_" + fileName; return Path.Combine(GetDirectory(filePath), name); } #endregion } public class LBCUploadFile:UploadFile,IUploadFile { /// <summary> /// 验证上传文件的扩展名 /// </summary> /// <param name="postFile">System.Web.HttpPostedFile对象</param> /// <returns></returns> public override bool CheckFileType(System.Web.HttpPostedFile postFile) { IList<string> fileType = new List<string>(); fileType.Add("image/gif"); fileType.Add("image/jpeg"); fileType.Add("image/png"); return fileType.Contains(postFile.ContentType); } }