gin base64 转 图片 直接显示在前端

环境

  • go 1.17.1
  • gin 1.7.7

后端代码

package main

import (
	"encoding/base64"
	"github.com/gin-gonic/gin"
)

func main() {
	engine := gin.Default()
	imageBase64 := "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAB2AAAAdgB+lymcgAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAArfSURBVHic7Zt9bN1VGcc/z/n9fve177tdt65rt7Vd6bqubN2QOTY2GC+KIA62oYCBKROjKKgxMZGAMcQYQVRQA+IQjDHZYBpMBORFDYKgotO5GWEblg0oe+1623t7X37n+Me229v2tvd9krhvcpOe3znPy3nOOc/veZ7zK5zBGfxfQ6bq3Lq5q9nRrNCGoIj0JY9X/mHDtj9GT5dyheCBy3sDYZ+7AqVbMAw7xnnx89tefXOy8RkNsP3mRdNJ6vswXA2otK6jxpivr9uy67sCptTKFwMDcs+GnlsFvgrUpXVpA495jPrs57b97dB4ugkGeHxz50xxrT8CLZMKM+bBdVt23fxeMYIB+faGRQ+A3DTFsP+4Jrn8y9t29ac/VONHqaT1MFNMHkBENv/iE923F6Zu6XHv+p7bs0weYI4l9sPjH47ZAY/fuGiJKP1qjnINRq5ft2Xnz3JVtBy4e+PZ14oxPyWLPzsFZWTxbdt27Ei10ztFzOo8ZAtitjy+qWtNHjQlxd3re84TY35MjpMH0IrV6e0xBjBi2vLUwSMi27bf1DM/T7qice9V3fNE2A5486EzRrent8fuAAgVoMs0tPvk1hsX1xdAWxC+vb6rTlvqSaAAmTJmjuOdYCEGAJhnq8T2X9/SltdqFII713d5jNiPAQXtOjVujmMNIAUbAOC8WMT3iMnjPOYLA1Kl7IeAwv2OmWoHmKIMgIGN2z+58I5ieEyFezb23GkM1xfDw4gZM0d7XH8dRaBuekV86dq2a9xNy7Iawe3+1iEsb/3w4YOIGztU/fLmrOc5Gtd7fvVKNHbgsC7mqE1Lb6R2w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P++RZaO/Nk78BuPE/nnFGGrRbxiCbGqVWTaNSKC5fGiTl7KRYaHeeutA4xEc/vS3uM41NTW4vN6AMF1XeKJeEZ1rOQwtYeeourYi4jJfBtsxGKoqpcjM6+MayuwtHXR0p0pXccPPvbm3i8A94gexonsxkr0o5LHAUFbFSQ9s0kGOjAq9+1njGFfXx/JZDL7YECUUFtbi8oj6rOSYYLhnfiG38BOnghuk04tMV8TQ9WLce1KxHBba0/vd8bIysTs2P49j2KkqM/RxiMajbL/7beznnMBKquq8XhKXRSRR9oWLblh/NOMJq55d+ATYJ4opXi/309TYyOWNXn2LUrKNHmeMv7KT2WUORmFef117zGvPCrIhlJqorVhYHCAoeEI8XgCYzTKsvA6Hnx+H2W4n9hq/FUfb29vz1iMmFKaMUaO79/7dYN8hUl2S7E4MlDCZHQsNMhdrd2L7xCRSc9dTuYe6Nt7oRF+AkyeyxaIMhlgP4Yb2np6n882MKdVrWlpfS7hG+kUzF3Ae/m/xqIId8Us34JcJg8F3FkN7dvXkLT1LQbZTEHf649FiXbAQTAPKkneP6/73HfzISzY4xjzW3tw/5wLDfoKA6uArkL4FWgAA+wS+L0W88SBw+Hn16xZk1uQMQ4lc7n9/f1BbzLajnbnYFSloLMnFlqrw+FwLp92YkSiYnRYKfVG2LX29PT05Bavn8EZnMFU+C9DtV84ZQ3kRwAAAABJRU5ErkJggg=="
	imageBuffer, _ := base64.StdEncoding.DecodeString(imageBase64)
	engine.GET("/view", func(c *gin.Context) {
		_, _ = c.Writer.WriteString(string(imageBuffer))
	})
	_ = engine.Run()
}


前端代码

<img src="http://localhost:8080/view">

效果

gin base64 转 图片 直接显示在前端

上一篇:东华oj 2.回文数


下一篇:Mysql数据库基础操作