1. 程式人生 > >上傳圖片的相關代碼

上傳圖片的相關代碼

ict eas return load bst pat ret data 註冊

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 }

上傳圖片的相關代碼