asp.net fileupload1上传图片怎样将图片名保存到数据库中
这个是我自己的,每个人的情况可能有些不同,但大致是这样子的。页面显示的差不多是这个样子
后台的代码(点击上传)看下面:
string Photo_src ="photo/"+ FileUpload1.FileName;
string Photo_script = txt_photo_script.Text;
SqlConnection conn = new SqlConnection();
string constr = ConfigurationManager.ConnectionStrings["conn"].ToString();
conn.ConnectionString = constr;
conn.Open();
string str = "insert into photo (user_name,photo_name,photo_src,photo_script) values ('" + Session["user_name"].ToString() + "','" + FileUpload1.FileName + "','" + Photo_src + "','" + Photo_script + "')";
SqlCommand cmd = new SqlCommand(str, conn);
bool filesValid = false;
if (cmd.ExecuteNonQuery() > 0)
{
String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" };
for (int i = 0; i < restrictExtension.Length; i++)
{
if (fileExtension == restrictExtension[i])
{
filesValid = true;
}
}
if (filesValid == true)
{
this.Image1.ImageUrl = "photo/" + FileUpload1.FileName;
this.FileUpload1.SaveAs(Server.MapPath("./") + "photo" + "//" + FileUpload1.FileName);
Response.Write("<script>alert('图片上传成功');</script>");
this.FileUpload1.PostedFile.FileName;
this.FileUpload1.PostedFile.ContentType;
}
else
{
Response.Write("<script>alert('图片格式不对(图片后缀错误)!');</script>");
}
Photo_src = "";
Photo_script = "";
}