1 /// <summary> 2 /// Web文件上传简单处理 3 /// </summary> 4 public class UploadFileHandler 5 { 6 private readonly HttpPostedFileBase _baseFile; 7 8 public UploadFileHandler(HttpPostedFileBase baseFile) 9 { 10 if (_baseFile == null) 11 throw new ArgumentNullException("baseFile"); 12 _baseFile = baseFile; 13 } 14 15 /// <summary> 16 /// 文件后缀验证 17 /// </summary> 18 /// <param name="regexp">例如:jpg|jpeg|bmp|gif</param> 19 /// <returns>true:验证通过,false:格式不正确</returns> 20 private bool VerifySuffix(string regexp) 21 { 22 if (regexp.IndexOf('|') == -1) throw new ArgumentException("验证文件类型表达式格式不正确"); 23 string regExp = string.Format(@"^.*?.({0})$", regexp); 24 var suffix = GetFileSuffix(); 25 return !string.IsNullOrEmpty(suffix) && Regex.IsMatch(suffix, regExp); 26 } 27 28 /// <summary> 29 /// 获取文件后缀名,包含"." 30 /// </summary> 31 /// <returns></returns> 32 private string GetFileSuffix() 33 { 34 return Path.GetExtension(_baseFile.FileName); 35 } 36 37 /// <summary> 38 /// 文件夹目录处理 39 /// </summary> 40 /// <param name="path"></param> 41 private static void DirectoryProcess(string path) 42 { 43 if (!Directory.Exists(path)) Directory.CreateDirectory(path); 44 } 45 46 /// <summary> 47 /// 保存上传文件,设置文件格式 48 /// </summary> 49 /// <param name="suffixs">例如:jpg|jpeg|bmp|gif </param> 50 /// <returns></returns> 51 public string Save(string suffixs) 52 { 53 return Save(null, null, null, suffixs); 54 } 55 56 /// <summary> 57 /// 保存上传文件,设置上传文件名称 58 /// 默认路径为System.IO.Directory.GetCurrentDirectory() 59 /// </summary> 60 /// <param name="getNameFunc">设置上传文件名称方法</param> 61 public string Save(Func<string> getNameFunc) 62 { 63 return Save(null, null, getNameFunc); 64 } 65 66 /// <summary> 67 /// 保存上传文件,设置上传文件名称和文件格式 68 /// 默认路径为System.IO.Directory.GetCurrentDirectory() 69 /// </summary> 70 /// <param name="suffixs"> </param> 71 /// <param name="getNameFunc">设置上传文件名称方法</param> 72 public string Save(string suffixs, Func<string> getNameFunc) 73 { 74 return Save(null, null, getNameFunc, suffixs); 75 } 76 77 /// <summary> 78 /// 保存上传文件,设置上传文件名称,路径为HttpContext.MapPath 79 /// </summary> 80 /// <param name="httpContext">当前请求处理的上下文对象</param> 81 /// <param name="virtualPath">虚拟目录</param> 82 /// <param name="getNameFunc">获取上传文件名称</param> 83 /// <param name="suffixs">例如:jpg|jpeg|bmp|gif </param> 84 /// <returns></returns> 85 public string Save(HttpContext httpContext, string virtualPath, Func<string> getNameFunc = null, string suffixs = "txt|docx|doc|jpg|jpeg|bmp|gif|xlsx|xls") 86 { 87 string suffix = GetFileSuffix(); 88 89 if (!VerifySuffix(suffixs)) throw new ArgumentException("文件格式不正确"); 90 91 string path; 92 93 if (httpContext == null || string.IsNullOrEmpty(virtualPath)) 94 path = Directory.GetCurrentDirectory(); 95 else 96 path = httpContext.Server.MapPath(virtualPath); 97 98 string fileName = getNameFunc == null ? Guid.NewGuid().ToString() : getNameFunc(); 99 100 if (string.IsNullOrEmpty(fileName)) throw new ArgumentException("设置上传文件名方法错误"); 101 102 string fullFileName = path + fileName + suffix; 103 DirectoryProcess(path); 104 _baseFile.SaveAs(fullFileName); 105 return fullFileName; 106 } 107 }