Render an image from base64 in golang

huangapple go评论88阅读模式
英文:

Render an image from base64 in golang

问题

我可以帮你翻译这段代码。这段代码是用golang编写的,用于从base64中渲染图像(这里是Twitter图标)。代码中定义了一个名为pix的函数,用于处理HTTP请求。在函数中,它首先获取名为"csrftoken"的cookie,并将其值打印出来。然后,它设置响应头的Content-Type为image/jpeg,并使用base64.StdEncoding.DecodeString函数将base64字符串解码为字节切片。如果解码过程中出现错误,它会返回一个内部错误的HTTP响应。最后,它设置Content-Length头,并将解码后的字节切片写入响应体。

在main函数中,它将/pix/路径映射到pix函数,并监听端口9080上的HTTP请求。

根据你的描述,当你访问URL时,它下载一个包含错误的文件,而不是显示图像。可能的原因是base64字符串不正确或解码过程中出现了错误。你可以检查base64字符串是否正确,并确保它代表一个有效的JPEG图像。另外,你还可以检查解码过程中是否有任何错误,并查看错误的详细信息以找出问题所在。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I would like to render an image from base64 in golang (here the twitter icon)

package main

import (
	base64 "encoding/base64"
	"fmt"
	"io"
	"net/http"
	"strconv"
)

func pix(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
	var cookie *http.Cookie
	cookie, err := r.Cookie("csrftoken")
	if err != nil {
		fmt.Printf("error")
		fmt.Println(err)
	}
	fmt.Printf(cookie.Value)
	w.Header().Set("Content-Type", "image/jpeg")
	p, err := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAADMUlEQVRYw+2YTUgUYRjHZzOJIoNA+rrUyYNIRQgRHaLo4qFDBEGeunSxS9TFU0QEnhIh6IvokrUzO2uamRmbG6XmR/mVaKZpZVbYvvO143zszsxOz+yahNm+785sITEP72F3Z+adH8/zf5+PpagwtxKXj+Vj+Vg+lo/lY+W+WI4KpddKwWIQFUSF97nNLcLGZt75SiOHchEXfskDVmYjlowpiEoei3UT2ljcFJOpOd169C1Z2SuvgsdpB7cgzB16EV/byGM2xDIVPxQujKmBDF/2m2l0vFvmEin7N2v8kiiPiOeGlGHRvP1RdxA9eYtGR7pk2Pf6lI7RCoP2RaWkZWe3fsFc18hvesAHPGEFUc24ltnx3kyiCJwfRMs6dTXLdSIjO9Osal18qzKfE5V9coDxhlU7qS3uOyiaB55JDtkS2TKoLCLaOLPS4b02pQdCHiUfRKf653/d2kjZN6f10jYxI2EnrGk5H+2WsVi6ZZ8fVSmGQKaYyyFuR6ugmUtVrJo2C7HokeGq8447sYpOPBbo3XFzKC95626sZlz905sUM9XLGbXvtKtTOhZrQDApkhNNkiAOPo/viojh2YSZsj1aF2eQ5n2stuomNQjiiGQanrFufdCXP8gu8tbhjridJ6saVPKExXJrwlwfb3pnAg2Ut0tEBZFI8gza81Tik15DCDIoINQ7aQdBo90RMfrdwNaWLFY9opJGkBQrhCA/HXspQ8W1XHkN6vfWFiGH9ouwhdpJUFuy2JX3eg6uyqENpNHZYcUd02jcLMI2WO67UwZVv1G1HLMq3L83KuEbLPdY7IL2L42p0MMQiuzkq/ncwucOi6qPbWkWoPfCUsENpweUnP1EmE4XGhgagT72RyXolkSCHBbTU3By3fgJj8VyJW3CmSHl8oTWMJuYUUizVvtcsuyJ6J4J663CMLevXar/lJgnKNSgbphzKjriTn5i0F8eX9ODXnEzf6JHvjGtv+aNGdWCOEKnJRmpr5oFVQV8WTWglIKHMlPhv5uqQ1xGYfB5fRMPo+n2VmFbi7ChiS9oWBhZvXrI01TNLg7yPxt51v9rxMfysXwsH8vH+g+wfgDUr+5LcyNV4AAAAABJRU5ErkJggg==")
	if err != nil {
		http.Error(w, "internal error", 500)
		return
	}
	w.Header().Set("Content-Length", strconv.Itoa(len(p))) //len(dec)
	io.WriteString(w, string(p))
}

func main() {
	http.HandleFunc("/pix/", pix)
	err := http.ListenAndServe(":9080", nil)
	if err != nil {
		fmt.Println(err)
	}
}

But it doesn't display anything and when I try to go to the URL; it downloads a file that contains error. Anyone have any idea why this is?

答案1

得分: 6

您正在向客户端写入除图像以外的数据。具体来说,是这一行代码:

fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])

您的浏览器试图将内容呈现为JPEG格式,但由于有额外的数据,所以无法成功,因此会提示您下载它。删除这行代码,图片将能正确显示。

您还应该遵循@Mellow Marmot的建议,使用w.Write(p)而不是io.WriteString(w, string(p))

英文:

You are writing data to client other than the image. Specifically, this line:

fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])

You browser tries to render the content as a JPEG, but fails because of this extra data, so it prompts you to download it instead. Remove it and the picture will be displayed correctly.

You should also follow @Mellow Marmot's suggestion and use w.Write(p) instead of io.WriteString(w, string(p)).

huangapple
  • 本文由 发表于 2016年12月14日 22:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/41144656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定