【发布时间】:2018-09-19 22:41:24
【问题描述】:
我正在尝试创建一个测试网站,我可以在其中上传图像和一些文本到数据库中,并在“预览”图像控件中检索图像。 我已经设法将这些元素存储为 varchar 和 blob(MEDIUMBLOB).(MySql)
但是我在尝试在图像控件中显示存储的图像时遇到了麻烦。
代码如下:
MySqlConnection conn;
MySqlCommand comand;
String queryStr;
MySqlDataAdapter daa;
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile postedFile = FileUpload1.PostedFile;
string fileName = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(fileName);
int fileSize = postedFile.ContentLength;
try
{
if (fileExtension.ToLower()==".jpg" || fileExtension.ToLower() == ".png")
{
AgregarCiudad();//add city method in spanish
Response.Write("Exito");
MostrarImagen();//show image method also in spanish lol
}
else
{
Response.Write("Solo Imagenes .jpg o .png de 15 megabites o menos.");
}
}
catch (FileNotFoundException fFileNotFound)
{
//log the exception the specified fFileNotFound variable has to be put in a string, label or response.
Response.Write("Error." + fFileNotFound.Message);
}
catch (Exception)
{
Response.Write("Error");
}
}
private void AgregarCiudad()
{
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["WebAppConnString"].ToString();
conn = new MySqlConnection(connString);
conn.Open();
queryStr = "";
queryStr = "INSERT INTO testingdb.country (Relation, Name, ImageStock)" +
"VALUES(?Relation, ?Name, ?ImageStock)";
comand = new MySqlCommand(queryStr, conn);
comand.Parameters.AddWithValue("?Relation", TextBox1.Text);
comand.Parameters.AddWithValue("?Name", TextBox2.Text);
comand.Parameters.AddWithValue("?ImageStock", FileUpload1);
comand.ExecuteReader();
conn.Close();
}
正如你在这里看到的,我不知道现在还能做什么,可以做些什么来让这个方法在 Image 控件 (Image1) 中显示最近存储的图像?
private void MostrarImagen()
{
String selectQuery = "SELECT ImageStock FROM testingdb.country WHERE
Name ='" + TextBox2.Text + "'";
comand = new MySqlCommand(selectQuery, conn);
MySqlDataReader reader;
reader = comand.ExecuteReader();
Image1.ImageUrl = selectQuery;
conn.Close();
}
【问题讨论】: