[英]How can i show an image from a database in mysql(blob), to an “Image” control in a web page
I'm trying to make a test website where i can upload an image and some text in a database and retrieve the image in a "preview" Image control. 我正在尝试创建一个测试网站,可以在其中上传图像和数据库中的一些文本,并在“预览”图像控件中检索图像。 I already managed to store these elements as varchar and blob(MEDIUMBLOB).(MySql) 我已经设法将这些元素存储为varchar和blob(MEDIUMBLOB)。(MySql)
However i'm having trouble trying to show the stored image in the Image control. 但是我在尝试在Image控件中显示存储的图像时遇到了麻烦。
here's the code: 这是代码:
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();
}
as you can see here, i don't know what else to do now, what could be done to make this method to show the recently stored image in the Image control (Image1)? 如您在这里看到的,我不知道现在该怎么办,可以做些什么来使此方法在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();
}
I am supposing that you are receiving the blob data from the database. 我假设您正在从数据库接收Blob数据。 After that try something like this: 之后,尝试如下操作:
string base64String = Convert.ToBase64String(your blob);
Image1.ImageUrl = "data:image/png;base64," + base64String;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.