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();
在当前页面如何插入头像。
最新推荐文章于 2021-06-04 12:44:05 发布