在当前页面如何插入头像。

来源:互联网 发布:下载天猫淘宝商城 编辑:程序博客网 时间:2024/04/28 05:00

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();

原创粉丝点击