using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Text; namespace Mtxfw.Utility { /// /// 文件上传 By Telesa 2011.02.29 /// public class UploadFile { #region 私有成员 private HttpPostedFile _HttpPostedFile; private string _InPath; private string _UpType; private bool _IsBool; private string _UploadPath; private string _Message; private int _UpSize; private string _FileName; private long _FileSize; private string _FileType; #endregion #region 构造函数 /// /// 构造函数 /// /// 上传控件 /// 上传路径 /// 上传类型逗号分隔:jpg,gif,bmp public UploadFile(HttpPostedFile PostFile, string isPath, string upType, int upSize) { this._HttpPostedFile = PostFile; this._InPath = isPath; this._UpType = upType; this._UpSize = upSize; } #endregion #region 私有方法 /// /// 生成GUID字符串 /// private string getGuidString() { return Guid.NewGuid().ToString().Replace("-", "").ToUpper(); } /// /// 获取上传文件的扩展名 不带 "." /// private string getExtension(string file) { string name = Path.GetExtension(_HttpPostedFile.FileName); return name.Substring(name.LastIndexOf(".") + 1).ToLower(); } /// /// 获取上传文件的原始名称 /// private string getName(string file) { return file.Substring(file.LastIndexOf("/") + 1).ToLower(); } /// /// 判断上传的文件类型是否是允许的 /// private bool hasType(string[] array, string type) { bool isType = false; foreach (string str in array) { if (type.IndexOf(str) != -1) { isType = true; //如果上传的文件类型是允许的则跳出循环 break; } } return isType; } //按年月日生产文件夹 格式 \YYYY\MM\DD private string getDateString() { StringBuilder sb = new StringBuilder(); sb.Append(DateTime.Now.Year); sb.Append("-"); sb.Append(DateTime.Now.Month); sb.Append("-"); sb.Append(DateTime.Now.Day); sb.Append("/"); return sb.ToString(); } #endregion #region 公有方法 /// /// 上传文件 并判断是否上传成功 /// /// public bool StatrUpFile() { string isFileName = getExtension(_HttpPostedFile.FileName); //获取文件扩展名 _FileType = getExtension(_HttpPostedFile.FileName); _FileSize = _HttpPostedFile.ContentLength; string strContentType = _HttpPostedFile.ContentType.ToLower(); //获取文件的原始文件名 _FileName = getName(_HttpPostedFile.FileName); string[] typeList = _UpType.Split(','); //限制上传的类型集合 _IsBool = hasType(typeList, isFileName); if (!String.IsNullOrEmpty(_HttpPostedFile.FileName)) { if (_HttpPostedFile.ContentLength < (_UpSize * 1024)) { if (_IsBool) { if (strContentType.IndexOf("jpg") != -1 || strContentType.IndexOf("jpeg") != -1 || strContentType.IndexOf("gif") != -1 || strContentType.IndexOf("png") != -1 || strContentType.IndexOf("bmp") != -1) { string FileType = _HttpPostedFile.FileName.Substring(_HttpPostedFile.FileName.LastIndexOf(".")).ToLower(); try { string DateString = getDateString(); string GuidName = getGuidString(); string InPathName = _InPath + DateString; //如果文件夹不存在则创建 if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(InPathName))) { Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(InPathName)); } //创建GUID图片名 string Final_Name = string.Format("{0}.{1}", GuidName, isFileName); //上传 _HttpPostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(InPathName + Final_Name)); if (!Mtxfw.Utility.Common.IsAllowedExtension(System.Web.HttpContext.Current.Server.MapPath(InPathName + Final_Name))) { File.Delete(System.Web.HttpContext.Current.Server.MapPath(InPathName + Final_Name)); _Message = ""; return false; } else { _UploadPath = DateString + Final_Name; return true; } } catch (Exception e) { _Message = e.Message; return false; } } else { _Message = "文件上传格式错误哦,请上传: " + _UpType + " 格式文件"; return false; } } else { _Message = "文件上传格式错误哦,请上传: " + _UpType + " 格式文件"; return false; } } else { _Message = "上传的文件不能大于 " + _UpSize + " KB"; return false; } } else { _Message = "请选择要上传的文件!"; return false; } } #endregion #region 公共属性 /// /// 获取文件上传后的路径 /// public string UploadPath { get { return _UploadPath; } } /// /// 异常信息 /// public string Message { get { return _Message; } } /// /// 获取上传文件的原始文件名 /// public string FileName { get { return _FileName; } } /// /// 获取上传文件的类型 /// public string FileType { get { return _FileType; } } /// /// 获取上传文件字节流的长度 /// public long FileSize { get { return _FileSize; } } #endregion } }