欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-03-27 16:14:31
FileUpload控件是.net自带的控件,相信大家上传文件的时候在不借助第三方控件时还是非常方便的,现在博主就拿实际项目中总结的通用方法给大家分享一下,相信对初学者还是很有帮助...
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>版权所有https://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;  
            }  
<ol start="1" class="dp-c" style="padding:0px; border:none; list-style-position:initial; color:rgb(92,92,92); font-family:Consolas,'Courier New',Courier,mono,serif; line-height:26px; margin:0px 0px 1px 45px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">版权:求知域http:</span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">//www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>  
            //扩展名不正确  
            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;  
            }  
<ol start="1" class="dp-c" style="padding:0px; border:none; list-style-position:initial; color:rgb(92,92,92); font-family:Consolas,'Courier New',Courier,mono,serif; line-height:26px; margin:0px 0px 1px 45px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">版权:求知域http:</span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">//www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>  
            //记录文件绝对路径  
            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);  
            }  
<ol start="1" class="dp-c" style="padding:0px; border:none; list-style-position:initial; color:rgb(92,92,92); font-family:Consolas,'Courier New',Courier,mono,serif; line-height:26px; margin:0px 0px 1px 45px!important"><li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"><span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">版权:求知域http:</span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">//www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>  
            //上传至服务器  
            fu.SaveAs(fileRootPath);  
  
            //返回文件绝对路径  
            return fileRootPath;  
        }