Asp.net_使用FileUpload控件上传文件通用方法分享

来源:互联网 发布:ios手机编程就业 编辑:程序博客网 时间:2024/06/05 22:48

FileUpload控件是.net自带的控件,相信大家上传文件的时候在不借助第三方控件时还是非常方便的,现在博主就拿实际项目中总结的通用方法给大家分享一下,相信对初学者还是很有帮助的(ls_man)。

/// <summary>使用FileUpload控件上传文件</summary>        /// <param name="page">this</param>        /// <param name="path">文件保存目录,相对路径示例:"~/UploadFile",绝对路径示例:"E:\UploadFile",提示:web路径中用斜杠,文件系统中用反斜杠</param>        /// <param name="fu">FileUpload</param>        /// <param name="checkFileName">是否检查文件名,true表示不允许文件名重复,false则文件名加时间重命名</param>        /// <param name="allowTypes">允许上传的文件类型,扩展名,例:"xls",不限制类型传null即可</param>        /// <returns>文件绝对路径</returns>        /// <remarks>版权所有http://blog.csdn.net/ls_man</remarks>        public static string UploadFileToServer(Page page, String path, FileUpload fu, Boolean checkFileName, params String[] allowTypes)        {            //记录文件名            string fileName = fu.FileName;            //是否选择文件            if (string.IsNullOrEmpty(fileName))            {                MsgBox.Alert(page, "请先选择文件!");                return null;            }            //记录文件扩展名            string fileType = fileName.Substring(fileName.LastIndexOf('.') + 1);            //判断扩展名是否允许            bool typeRight = false;            //记录允许上传的文件类型            string allowType = "";            //是否指定文件类型            if (allowTypes != null)            {                //遍历允许文件类型数组                for (int i = 0; i < allowTypes.Length; i++)                {                    //已判断为允许则不再判断                    if (!typeRight)                    {                        //扩展名大小转换判断是否符合要求                        if (fileType == allowTypes[i] || fileType.ToLowerInvariant() == allowTypes[i] || fileType.ToUpperInvariant() == allowTypes[i])                        {                            //符合要求则设置为允许                            typeRight = true;                        }                    }                    //记录允许上传的文件类型                    allowType += "." + allowTypes[i] + " | ";                }                //删除最后一个分隔符                allowType = allowType.Remove(allowType.LastIndexOf("|"));            }            //未指定文件类型时            else            {                //直接设置为允许                typeRight = true;            }            //扩展名不正确            if (!typeRight)            {                //提示允许上传的文件类型                MsgBox.Alert(page, "文件格式不正确,请选择扩展名为[  " + allowType + " ]的文件!");                return null;            }            //是否可以正常获取文件            if (fu.PostedFile.ContentLength == 0)            {                MsgBox.Alert(page, "找不到选择的文件,请重新选择!");                return null;            }            //目录绝对路径            string dirRootPath = "";            //文件路径异常处理            try            {                //如果路径是相对路径                if (!Path.IsPathRooted(path))                {                    //目录相对路径转绝对路径                    dirRootPath = HttpContext.Current.Server.MapPath(@"" + path + "/").Trim();                }                else                {                    //保存路径                    dirRootPath = path;                }                //文件上传目录是否存在                DirectoryInfo dirInfo = new DirectoryInfo(dirRootPath);                if (!dirInfo.Exists)                {                    //不存在则创建此目录                    dirInfo.Create();                }            }            catch (Exception pathError)            {                //异常弹窗提示                MsgBox.Alert(page, "错误:" + pathError.Message);                return null;            }            //记录文件绝对路径            string fileRootPath = "";            //需要检查文件名是否重复时            if (checkFileName)            {                //文件绝对路径                fileRootPath = Path.Combine(dirRootPath, fileName);                //文件名已存在                if (File.Exists(fileRootPath))                {                    //提示改名                    MsgBox.Alert(page, "服务器已存在同名文件,请修改文件名后重试!");                    return null;                }            }            else            {                //选择的文件按时间重命名                string newFileName = fileName.Remove(fileName.LastIndexOf(".")) + DateTime.Now.ToString("yyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + "." + fileType;                //文件绝对路径                fileRootPath = Path.Combine(dirRootPath, newFileName);            }            //上传至服务器            fu.SaveAs(fileRootPath);            //返回文件绝对路径            return fileRootPath;        }

【End】

奋斗更多精彩博文请移步博客主页:http://blog.csdn.net/ls_man

奋斗更多精彩分享请收听博主腾讯微博@ls_man:http://t.qq.com/ls_man


原创粉丝点击