FileUpload上传之文件大小与类型的限制方法

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string savePath = Server.MapPath("/Upload");//保存的路径

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string savePath = Server.MapPath("/Upload");//保存的路径
            if (FileUpload1.HasFile)
            {
                try
                {
                    string fileName = Server.HtmlEncode(FileUpload1.FileName);
                    string extension = Path.GetExtension(fileName);//获得后缀

                    if (FileUpload1.PostedFile.ContentLength > 100000)//文件大小限制
                    {
                        lblPic.Text = "文件不能超过100K";
                        return;
                    }

                    if ((extension == ".jpg") || (extension == ".gif") || (extension == ".png") || (extension == ".jpeg"))
                    {
                        FileUpload1.SaveAs(savePath + fileName);
                    }
                    else
                    {
                        lblPic.Text = "限上传:gif,jpg,jpeg,png格式的图片";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    //
                }            
           
            }

中<system.web>中加入

      <httpRuntime executionTimeout="90" maxRequestLength="200000" useFullyQualifiedRedirectUrl="false" />

上传是先校验这里的大小的。默认最大为4M。上传超过此大小的就会报错了。

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注