上传图片的相关代码

1            if (upload1.HasFile)//判断控件是否有文件路径
 2                 {
 3                     string filename = upload1.FileName;//取得文件名
 4                     filename = filename.Substring(filename.LastIndexOf(".") + 1);//取得后缀
 5                     if (filename.ToLower() == "jpg" || filename.ToLower() == "gif")//判断类型
 6                     {
 7                         string img = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + filename;
 8                         upload1.SaveAs(Server.MapPath("images/") + img);
 9                         string picture = ("images/") + img;
10                         //传到根目录的images文件夹+重命名的文件名,也可以用原来的图片的名称,自己定。上传成功;
11                         string sql = "insert into tabUsers values('" + ID + "','" + name + "','" + hspwd + "','" + sex + "','" + year+ "','"+mail+"','" + question1 + "','" + hsanswer + "','" + picture + "')";
12                         int result2 = Class.Put(sql);
13                         if (result2 == 1)
14                             Binddata();
15                         else if (result2 != 1)
16                         {
17                             Response.Write("<script>alert('注册失败!');</script>");
18                             return;
19                         }
20                     }
21                     else
22                     {
23                         Response.Write("<script>alert('图片格式只支持jpg和gif');</script>");
24                         return;//提示错误
25                     }
26                 }
27                 else
28                 {
29                     Response.Write("<script>alert('请选相片!');</script>");
30                     return;//提示错误
31                 }

 

posted @ 2018-07-06 10:36  newlore  阅读(140)  评论(0编辑  收藏  举报