protected void Button1_Click(object sender, EventArgs e)
{
Boolean fileOK = false;
string path = Server.MapPath("~/UpLoadFile控件使用/UploadedImages/");
if (FileUpload1.HasFile)
{
string fileExtension = (FileUpload1.FileName);
string[] allowedExtensions = {".jpg",".jpeg",".txt",".png",".doc",".ppt",".gif"};
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
string picpath = "UpLoadFile控件使用/UploadedImages/" + FileUpload1.FileName;
string proname = TextBox1.Text;
string prodec = TextBox2.Text;
DateTime date = DateTime.Now;
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlserver"]);
SqlConnection con = new SqlConnection(settings);
con.Open();
string sql = "insert into
Products(ProName,Adddate,Description,imgurl) values('" + proname + "','" + date + "','" + prodec + "','" + path + "')";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('商品添加成功!')</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('文件上传出现问题!')</script>");
}
}
else
{
Response.Write("<script>alert('上传文件类型错误!')</script>");
}
TextBox1.Text = "";
TextBox2.Text = "";
}
然后你在建个查看图片的页面
private void aa()
{
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlserver"]);
SqlConnection con = new SqlConnection(settings);
con.Open();
string sql = "select * from Products";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader da = cmd.ExecuteReader();
FormView1.DataSource = da;
FormView1.DataBind();
con.Close();
}
把图片绑定的formview控件中就行了