英文:
Image blob to base64
问题
我正在尝试从数据库中读取一个 blob,并将其作为 base64 图像显示在浏览器中(我认为这是唯一的显示方式)。
我的代码如下:
rows, err := database.Conn.Query("SELECT a.id, a.name, a.creationdata, b.logo, c.name FROM guilds a, cloaka_guilds b, players c WHERE a.ownerid = c.id AND b.guild_id = a.id ORDER BY creationdata DESC")
if err != nil {
return nil, err
}
guild_list := []*Guild{}
for rows.Next() {
guild := &Guild{}
rows.Scan(&guild.Id, &guild.Name, &guild.Creationdata, &guild.Logo, &guild.Owner)
guild.Logo_str = base64.StdEncoding.EncodeToString(guild.Logo)
guild_list = append(guild_list, guild)
}
return guild_list, nil
b.logo 是一个图像的 []byte。我对此非常确定。为了更加确定,IntelliJ IDEA 在数据库行上显示:
88x88 Image 1.68K PNG
在浏览器中,我使用以下方式进行渲染:
<img src="data:image/jpeg;base64,[[ $element.Logo ]]">
但是显然有些问题,因为我得到了这个:
<img src="data:image/jpeg;base64,[137%2080%2078%2071%2013%2010%2026%20.........]">
至于结构体 guild.Logo 是 []byte 类型。
英文:
Im trying to read a blob from database and show it to browser as a base64 image (I think its the only way to show it)
My code is the following
rows, err := database.Conn.Query("SELECT a.id, a.name, a.creationdata, b.logo, c.name FROM guilds a, cloaka_guilds b, players c WHERE a.ownerid = c.id AND b.guild_id = a.id ORDER BY creationdata DESC")
if err != nil {
return nil, err
}
guild_list := []*Guild{}
for rows.Next() {
guild := &Guild{}
rows.Scan(&guild.Id, &guild.Name, &guild.Creationdata, &guild.Logo, &guild.Owner)
guild.Logo_str = base64.StdEncoding.EncodeToString(guild.Logo)
guild_list = append(guild_list, guild)
}
return guild_list, nil
b.logo holds an image []byte. Im sure of that. To be even more sure IntelliJ IDEA shows on the database row
> 88x88 Image 1.68K PNG
On the browser im rendering with something like
<img src="data:image/jpeg;base64,[[ $element.Logo ]]">
But something is clearly wrong since I get this
<img src="data:image/jpeg;base64,[137%2080%2078%2071%2013%2010%2026%20.........]">
As for the struct guild.Logo is []byte type
答案1
得分: 1
将图像呈现为base64格式时,不需要使用[[
符号。这是一个示例:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQV....
并且这是它的显示效果。
查看从数据库获取的数据。它应该是一个byteArray或者可以转换为byteArray的东西。有了byteArray,你可以使用以下代码对其进行base64编码:
sEnc := b64.StdEncoding.EncodeToString(byteArr)
请查看我的kbd,了解我是如何得到上述的base64图像的。
英文:
To present image as a base64 you do not need [[
symbols. Here is an example:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQV....
Take a look at the data you get from your database. It should be a byteArray or something that you can convert to byteArray. Having a byteArray you can base64 encode it with:
sEnc := b64.StdEncoding.EncodeToString(byteArr)
Take a look at my <kbd>Go playground</kbd> to see how I got the abovementioned base64 image.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论