ASP.net 上传文件类

public static  class FileUP
    {
        //maxLength以兆为单位
       public static   void MyUpload(FileUpload myFileUpload, int maxLength, string savePath, string saveName)
        {




            //检查是否有文件案      
            if (myFileUpload.HasFile)
            {
                // 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024      
                if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
                {
                    throw new Exception("只能上传小于" + maxLength + "M的文件!");
                }


                //取得上传文件之扩展文件名,并转换成小写字母      
                string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();




                try
                {
                    string path = savePath + (saveName == "" ? myFileUpload.FileName : saveName);
                    //存储文件到文件夹      
                    myFileUpload.SaveAs(path);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }


            }
            else
            {
                throw new Exception("请选择要上传的文件!");
            }
        }


       public static  void MyUpload2(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath, string saveName)
        {
            // 文件格式是否允许上传      
            bool fileAllow = false;


            //检查是否有文件案      
            if (myFileUpload.HasFile)
            {
                // 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024      
                if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
                {
                    throw new Exception("只能上传小于2M的文件!");
                }


                //取得上传文件之扩展文件名,并转换成小写字母      
                string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
                string tmp = "";   // 存储允许上传的文件后缀名      
                //检查扩展文件名是否符合限定类型      
                for (int i = 0; i < allowExtensions.Length; i++)
                {
                    tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";
                    if (fileExtension == allowExtensions[i])
                    {
                        fileAllow = true;
                    }
                }


                if (fileAllow)
                {
                    try
                    {
                        string path = savePath + (saveName == "" ? myFileUpload.FileName : saveName);
                        //存储文件到文件夹      
                        myFileUpload.SaveAs(path);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                else
                {
                    throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);
                }
            }
            else
            {
                throw new Exception("请选择要上传的文件!");
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值