简体   繁体   中英

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)

However i'm having trouble trying to show the stored image in the Image control.

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)?

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. After that try something like this:

string base64String = Convert.ToBase64String(your blob);
Image1.ImageUrl = "data:image/png;base64," + base64String;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM