Asp.net fileUpLoad怎么上传文件 (图片)

2024-12-30 14:00:06
推荐回答(2个)
回答1:

protected void Button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();

string fullname = FileUpload1.PostedFile.FileName;
FileInfo fi = new FileInfo(fullname);
string name = fi.Name;
string type = fi.Extension;
if (".gif" == type || ".jpg" == type || ".Gif" == type || ".JPG" == type)
{
//定义保存图片的路径
string savepath = Server.MapPath("~/image");
//保存上传的图片
FileUpload1.PostedFile.SaveAs(savepath + "\\" + name);
string picurl=savepath+"\\"+name;
Response.Write("");
try
{
SqlConnection conn = new SqlConnection(constr);
string instr = "insert into picture (picurl) values ('"+picurl+"') ";
conn.Open();
SqlCommand cmd = new SqlCommand(instr,conn);
cmd.ExecuteNonQuery();

Image1.ImageUrl = "image" + "/" + name;
}
finally
{
new SqlConnection().Close();
}
}
else
{
Response.Write("");
}
}

回答2:

图片上传到数据库只能是文件路劲,你前台显示的是文件的路劲,肯定是你的image设置错误或表达式错误!