我正在尝试将 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/