注册页面上传图片的两种方法。

两种方法:

if(FileUpload1.FileName !="")
            {
                string ImgName =FileUpload1.PostedFile.FileName;
                string ImgExtention =System.IO.Path.GetExtension(ImgName);
                int ImgSize =FileUpload1.PostedFile.ContentLength;
                string newname =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                string ImgPath =Server.MapPath("~/Ftb/");
                string ImgUrl ="~/Ftb/" + newname + ImgExtention;
                if (ImgExtention ==".gif" || ImgExtention == ".GIF" || ImgExtention ==".jpg" || ImgExtention == ".JPG" || ImgExtention ==".jpeg" || ImgExtention == ".JPEG")
                {

                    //图片尺寸
                    if (ImgSize / 512000< 1)
                    {
                        //数据添加
                        //DateTime now =DateTime.Now;
                        FileUpload1.PostedFile.SaveAs(ImgPath+ newname + ImgExtention);


                        string constr =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
                        SqlConnection con= new SqlConnection(constr);
                        con.Open();
                        DateTime itime =DateTime.Now;
                        string str ="insert into picture(txt,time) values('" + ImgUrl +"','"+itime+"')";//Imgurl就是图片的路径 上面定义的

                        SqlCommand cmd =new SqlCommand(str, con);

                        int i =cmd.ExecuteNonQuery();
                        if (i > 0)
                        {
                           Response.Write("<script>alert('添加成功')</script>");
                        }
                        else
                        {
                           Response.Write("<script>alert('添加失败')</script>");
                        }
                        con.Close();


                    }
                    else
                    {
                       ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片大小不能超过1M!');</script>");
                    }
                }
                else
                {
                   ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片格式不正确,只支持.gif .jpg .jpeg格式类型的图片!');</script>");
                }
            }
            else
            {
               ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('请选择要上传的图片!');</script>");
            }

 

 

======================================================================

方法2:

 

 

if(string.IsNullOrEmpty(FileUpload1.FileName))
            {
                ClientScript.RegisterStartupScript(this.GetType(),"", "<script>alert('请选择要上传的图片!');</script>");
                return;
            }
            string ImgName =FileUpload1.PostedFile.FileName;
            string ImgExtention =System.IO.Path.GetExtension(ImgName);
            int ImgSize =FileUpload1.PostedFile.ContentLength;
            string newname =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
            string ImgPath =Server.MapPath("~/Ftb/");
            string ImgUrl ="~/Ftb/" + newname + ImgExtention;
            if (ImgExtention !=".gif" && ImgExtention != ".GIF" &&ImgExtention != ".jpg" && ImgExtention != ".JPG"&& ImgExtention != ".jpeg" && ImgExtention !=".JPEG")
            {
               ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片格式不正确,只支持.gif .jpg .jpeg格式类型的图片!');</script>");
                return;
            }

            //图片尺寸
            if (ImgSize / 512000 >= 1)
            {
               ClientScript.RegisterStartupScript(this.GetType(), "","<script>alert('图片大小不能超过1M!');</script>");
                return;
            }
            //数据添加
            //DateTime now =DateTime.Now;
           FileUpload1.PostedFile.SaveAs(ImgPath + newname + ImgExtention);


            string constr =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            SqlConnection con = newSqlConnection(constr);
            con.Open();
            DateTime itime =DateTime.Now;
            string str = "insertinto picture(txt,time) values('" + ImgUrl + "','" + itime +"')";//Imgurl就是图片的路径 上面定义的

            SqlCommand cmd = newSqlCommand(str, con);

            int i =cmd.ExecuteNonQuery();
            if (i > 0)
            {
               Response.Write("<script>alert('添加成功')</script>");
            }
            else
            {
               Response.Write("<script>alert('添加失败')</script>");
            }
            con.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值