图像未绑定
本文关键字:绑定 图像 | 更新日期: 2023-09-27 18:21:54
我想将我的图像控件与sql数据库绑定。我将我的图像路径保存在sql数据库中,并从数据库中检索图像路径。当我通过断点检查时,它会向我显示图像的路径,但图像与它不绑定。这是我的代码
string imagepath = Server.MapPath("~/Pics/");
string serverfilename = Path.GetFileName(AsyncFileUpload1.PostedFile.FileName);
string fullpath = Path.Combine(imagepath, serverfilename);
string path = "~''Pics''" + serverfilename;
//string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
//string path = Server.MapPath("~/Pics/") + filename;
AsyncFileUpload1.SaveAs(fullpath);
// FileUpload1.PostedFile.SaveAs(path);
SqlCommand cmd = new SqlCommand("insert findfriend values('" + path + "','" + TextBox1.Text + "')", con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
SqlCommand GetImage = new SqlCommand("select * from findfriend where name='" + TextBox1.Text + "'", con);
GetImage.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(GetImage);
DataSet ds = new DataSet();
da.Fill(ds);
Image1.ImageUrl = ds.Tables[0].Rows[0][1].ToString();
请告诉我哪里做错了
我想您忘记将图像添加到页面中了。
Page.Controls.Add(yourImage);
如果您有一个已经存在的图像,请检查该图像是否已上传并存在于目录中。
数据库通常以以下格式标识路径:
string path="Images/Yourfolder/imagename".
保存到数据库时不要添加"~"。
例如:string path="~/Images/..."
只需在将其保存到数据库中时尝试更改路径即可。
如果要保存的路径有"//"或反斜杠"''",则它将无法识别文件夹。请保存并使用我上面提到的格式进行检查。
我使用下面的代码从fileupload控件中检索文件,然后将新路径存储在一个单独的字符串中,并将其传递给数据库。
filepath = Server.MapPath("~/Images/Gallery/" + uploadedFile.FileName);
uploadedFile.SaveAs(filepath);
newpath = "/Images/Gallery/" + uploadedFile.FileName;