简体   繁体   中英

Converting BLOB Data Into base64 and Then Display it

I am trying to convert BLOB data that's coming from MySQL database to Base64 and then displaying this to an jpg image.. here's the code am using:

using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection("S;Port=P;Database=DB;Uid=U;Pwd=P"))
            {
                connection.Open();
               MySql.Data.MySqlClient.MySqlCommand cmd = connection.CreateCommand();
               cmd.CommandText = "SELECT blobValue FROM Table WHERE blob_id = '333'";

               MySql.Data.MySqlClient.MySqlDataReader datr = cmd.ExecuteReader();
                if (datr.Read())
                {


                    byte[] xx = (byte[])datr.GetValue(0);

                    string base64String = Convert.ToBase64String(xx, 0, xx.Length);
                    Image1.ImageUrl = "data:image/jpg;base64," + base64String;

                    }

I guess am not having a problem with the code, but when running the code i get an empty picture, like this one --> 在此处输入图片说明

Please some help.

Use following code.

public string BlobToStringConverter(OracleDbType.Blob blobObject){
    if (blobObject != null){
        return  Encoding.UTF8.GetString((byte[])(blobObject));
    } else {
        return string.Empty; 
    }
}

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