当前位置: 代码迷 >> Web前端 >> 一个FileUpload类库 支持上 欢迎大家提出意见和建议
  详细解决方案

一个FileUpload类库 支持上 欢迎大家提出意见和建议

热度:126   发布时间:2012-09-02 21:00:34.0
一个FileUpload类库 支持下 欢迎大家提出意见和建议
  
今天出于项目需要 之前的类库出现很多冗余代码 闲暇之余自己下了一个FilesUpload类库 个人感觉而不错 欢迎大家提出建议
 
调用代码:

string str = "UpLoadFile/Img"; FilesUpload UpMain = new FilesUpload(); UpMain.FileUploadControl = FileUpload1; //设定上传的目录,成功后为存贮的路径“UpLoadFile/Img/************************.jpg” UpMain.UpLoadFilePath = str; //限定文件的格式必须以’|‘隔开 UpMain.AllowUpLoadFileExtension = ".zip|.jpg"; //设定文件的命名方式FilesUpload.RenameType 枚举类型,Guid:guid命名规则,Default:原文件命名规则(如果重复则为原文件命名+i,i为重复的个数),TimeNumber 时分秒毫秒命名规则(yyyyMMddhhmmssfff) //UpMain.RenameTypeList = FilesUpload.RenameType.TimeNumber; UpMain.UpLoad(); if (UpMain.UpLoadSuccess) { //成功后返回的文件扩展名(.jpg) //UpMain.UpLoadFileExtension //成功后返回的文件的存贮路径 “UpLoadFile/Img/************************.jpg” //UpMain.UpLoadFilePath //上传文件的大小 字节为单位 int i = UpMain.UploadFileSize; MessageBox.Show(Page, "上传成功!"); } else {

//失败时候的错误信息 //UpMain.UploadFileErrorMsg MessageBox.Show(Page, UpMain.UploadFileErrorMsg); }



 
public class FilesUpload
    {
        private FileUpload fileUploadControl;
        /// <summary>
        /// 上传控件
        /// </summary>
        public FileUpload FileUploadControl
        {
            get { return fileUploadControl; }
            set { fileUploadControl = value; }
        }
        private bool upLoadSuccess = false;
        /// <summary>
        /// 是否上传成功
        /// </summary>
        public bool UpLoadSuccess
        {
            get { return upLoadSuccess; }
        }
        private string upLoadFilePath;
        /// <summary>
        /// 上传路径,成功以后返回相对路径;
        /// </summary>
        public string UpLoadFilePath
        {
            get { return upLoadFilePath; }
            set { upLoadFilePath = value; }
        }
        private string upLoadFileExtension;
        /// <summary>
        /// 成功后返回文件的的格式,例如:".jpg"
        /// </summary>
        public string UpLoadFileExtension
        {
            get { return upLoadFileExtension; }
        }
        private string upLoadFileThumbnailPath;
        /// <summary>
        /// 缩略图路径
        /// </summary>
        public string UpLoadFileThumbnailPath
        {
            get { return upLoadFileThumbnailPath; }
            set { upLoadFileThumbnailPath = value; }
        }

        private int uploadFileSize;
        /// <summary>
        /// 上传成功返回文件的大小
        /// </summary>
        public int UploadFileSize
        {
            get { return uploadFileSize; }
        }
        private string uploadFileErrorMsg;
        /// <summary>
        /// 上传失败错误信息
        /// </summary>
        public string UploadFileErrorMsg
        {
            get { return uploadFileErrorMsg; }
        }
        private string allowUpLoadFileExtension = ".jpg|.gif|.png|.bmp|.psd|.swf|.doc|.docx|.rtf|.xls|.txt|.xlsx|.ppt|.pdf|.rar|.zip|.mpp|.mpd|.chm|.wma|.mp3|.wmv|.mid|.mpg|.avi|.asf|.mht|.mhtml|.htm|.html|.xml|.dwg|.pln";
        /// <summary>
        /// 限定的文件格式必须是".jpg|.gif"格式
        /// </summary>
        public string AllowUpLoadFileExtension
        {
            get { return allowUpLoadFileExtension; }
            set { allowUpLoadFileExtension = value; }
        }

        private int allowUploadFileSize = 31457280;

        public int AllowUploadFileSize
        {
            get { return allowUploadFileSize; }
            set { allowUploadFileSize = value; }
        }

        private RenameType renameTypeList = RenameType.TimeNumber;

        public RenameType RenameTypeList
        {
            set { renameTypeList = value; }
        }
        public enum RenameType
        {
            /// <summary>
            ///原文件名+上传时间时间
            /// </summary>
            Default,
            /// <summary>
            /// GUID命名规则
            /// </summary>
            Guid,
            /// <summary>
            /// 时间yyyyMMddhhmmss命名
            /// </summary>
            TimeNumber
        }
        public void UpLoad()
        {
            #region
            if (fileUploadControl.HasFile)
            {
                string fileName = fileUploadControl.PostedFile.FileName;
                upLoadFileExtension = Path.GetExtension(fileName).ToLower();
                if (fileUploadControl.PostedFile.ContentLength > allowUploadFileSize)
                {
                    upLoadSuccess = false;
                    uploadFileErrorMsg = string.Format("不允许上传大小超过{0}M 的文件!", allowUploadFileSize / 1048576);
                }
                else if (allowUpLoadFileExtension.Split('|').Where(p => p.Equals(upLoadFileExtension)).Count() == 0)
                {
                    upLoadSuccess = false;
                    uploadFileErrorMsg = string.Format("不允许上传格式为{0}的文件!", upLoadFileExtension);
                }
                else
                {
                    try
                    {
                        switch (renameTypeList)
                        {
                            case RenameType.Default:
                                upLoadFilePath = GetDefaultNameFilePath(upLoadFilePath, Path.GetFileNameWithoutExtension(fileName), upLoadFileExtension);
                                break;
                            case RenameType.Guid:
                                upLoadFilePath = string.Format("{0}/{1}{2}", upLoadFilePath, Guid.NewGuid().ToString("N").ToUpper(), upLoadFileExtension);
                                break;
                            case RenameType.TimeNumber:
                                upLoadFilePath = GetTimeNumberFilePath(upLoadFilePath, upLoadFileExtension);
                                break;
                            default:
                                break;
                        }
                        fileUploadControl.SaveAs(HttpContext.Current.Server.MapPath(string.Format("~/{0}", upLoadFilePath)));
                        uploadFileSize = fileUploadControl.PostedFile.ContentLength;
                        upLoadSuccess = true;
                    }
                    catch (Exception ex)
                    {
                        upLoadSuccess = false;
                        uploadFileErrorMsg = string.Format("上传过程出现错误:{0}", ex.Message);
                    }
                }
            }
            else
            {
                upLoadSuccess = false;
                uploadFileErrorMsg = "文件不允许为空!";
            }
            #endregion
        }
        /// <summary>
        /// 获得上传文件名原名+i,文件物理相对路径
        /// </summary>
        /// <param name="upLoadFilePath"></param>
        /// <param name="extensionName"></param>
        /// <returns></returns>
        public string GetDefaultNameFilePath(string upLoadFilePath, string fileName, string extensionName)
        {
            string path = string.Format("{0}/{1}", upLoadFilePath, fileName);
            int i = 1;
            while (File.Exists(HttpContext.Current.Server.MapPath(string.Format("~/{0}{1}", path, extensionName))))
            {
                path = string.Format("{0}/{1}{2}", upLoadFilePath, fileName, i);
                i++;
            }
            return string.Format("{0}{1}", path, extensionName);
        }
        /// <summary>
        /// 根据时间获得文件物理相对路径
        /// </summary>
        /// <param name="upLoadFilePath"></param>
        /// <param name="extensionName"></param>
        /// <returns></returns>
        private string GetTimeNumberFilePath(string upLoadFilePath, string extensionName)
        {
            string path = string.Format("{0}/{1}", upLoadFilePath, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
            while (File.Exists(HttpContext.Current.Server.MapPath(string.Format("~/{0}{1}", path, extensionName))))
            {
                Random rd = new Random();
                path = string.Format("{0}{1}", path, rd.Next(100, 999));
            }
            return string.Format("{0}{1}", path, extensionName);
        }
    }
 
  相关解决方案