Http 转到Fprint的多个response.WriteHeader调用
我想先把文字信息打印出来,然后在文字下面,再显示图像。 但我收到http:multiple response.WriteHeader调用错误 如何在一个页面中使用一个hadler提供图像和文本Http 转到Fprint的多个response.WriteHeader调用,http,go,Http,Go,我想先把文字信息打印出来,然后在文字下面,再显示图像。 但我收到http:multiple response.WriteHeader调用错误 如何在一个页面中使用一个hadler提供图像和文本 func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, world!") fp := path.Join("images", "gopher.png") http.ServeFile(w, r,
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
fp := path.Join("images", "gopher.png")
http.ServeFile(w, r, fp)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
您不能写入文本,然后调用ServeFile在文本后输出二进制图片 如果要为图像提供文本,请使用html,设置静态文件处理程序并使用html:
var tmpl = `<!doctype html>
<html>
<head>
<title>%s</title>
</head>
<body>
<h1>%s</h1>
<div><img src="images/%s"></div>
</body>
</html>
`
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, tmpl, "Hello, world!", "Hello, world!", "gopher.png")
}
func main() {
http.HandleFunc("/", handler)
http.Handle("/images/", http.FileServer(http.Dir("images/")))
http.ListenAndServe(":3000", nil)
}
是否有其他方法可以通过图像提供文本?@EPSILONsdfsdfdsf使用html。这适用于静态文件,但当我提供时,服务器找不到file@EPSILONsdfsdfdsf确实为文件服务器设置了正确的路径吗?您也可以将图像作为数据URI嵌入base64编码中,但我不知道具体如何实现。