go - 将Base64字符串转为PNG图片并响应为http Response - Go语言

标签 go

我正在尝试将 base64 编码转换为 png 图像并将图像输出为网络请求的响应。我可以在不在服务器中创建文件的情况下执行此操作吗?

http 的'ServeFile' 仅在图像保存为文件时起作用。但是,我想将 base64 字符串解码为图像数据,然后直接将其写入输出。

谢谢。

最佳答案

使用 base64.NewDecoder ,例如:

func Handler(res http.ResponseWriter, req *http.Request) {
    //in this example the client submits the base64 image, however
    // you can use any io.Reader and pass it to NewDecoder.
    dec := base64.NewDecoder(base64.StdEncoding, req.Body)
    res.Header().Set("Content-Typee", "image/png")
    io.Copy(res, dec)
}

关于go - 将Base64字符串转为PNG图片并响应为http Response - Go语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24220202/

相关文章:

data-structures - 用于解码的 golang 结构

windows - 确定是否可以在 windows 上删除文件

json - 在 Go 中将组合对象转换为 json

Golang 包也必须可以独立于 cli 使用

具有并发读者的 Golang 缓冲区

go - 无法通过 HTTP 代理 `go get` 存储库

select - 从非 chan 类型 time.Duration 接收

go - 找不到 Go 包

json - 从 golang 中的 json 文件中读取 slice

Go 应用程序(在 Docker 容器中)没有反射(reflect)页面上的更改?