将Base64字符串转换为PNG图像并作为http Response - Go语言进行响应

 认知天下微博 发布于 2023-01-09 11:58

我正在尝试将base64编码转换为png图像,并将图像作为Web请求的响应输出.我可以不在服务器中创建文件吗?

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

谢谢.

1 个回答
  • 使用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)
    }
    

    2023-01-09 12:00 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有