进行多次响应.WriteHeader调用Fprint

用户2671513:

我想先打印出短信,然后在文字下方显示图像。但是我遇到了http: multiple response.WriteHeader calls错误。

如何在一个页面中使用一个hadler来提供iamges和文本?

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)
}
OneOfOne:

您无法编写文本,然后调用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)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

多个响应。Go中的WriteHeader调用

Golang http:多个响应.WriteHeader调用

Golang:两次重定向并导致http:多次响应。WriteHeader调用

重定向返回http:多个响应.WriteHeader调用

多个响应。WriteHeader调用在非常简单的示例中吗?

“ http:多重响应.WriteHeader调用”的不良影响是什么?

如何处理多余的响应.WriteHeader调用以返回500

邮递员使用响应正文中的值进行多次 api 调用

非常零星的Go HTTP错误:多个响应。WriteHeader调用

多次调用的函数会本地响应

多次调用 ajax 会导致重复响应

Spring REST巨大的响应导致响应编写器被多次调用

如何阻止多次重新渲染进行多次 api 调用 useEffect?

linux使用watch命令进行多次调用

在单个 setState 调用中多次响应状态更新

多次调用mex函数后,matlab没有响应

如何内联(链调用)对类方法进行多次调用

春季并行对不同方法进行多次调用

列表上的嵌套点击事件正在进行多次调用

一次调用sed即可进行多次编辑

多次覆盖函数以进行继承调用堆栈日志记录

根据变量的长度多次进行突变调用

使用Mockito 2.7.5对同一模拟进行多次调用的ArgumentMatchers

如何避免从Jquery Autocomplete进行多次ajax调用?使用缓存

在angularjs的控制器内对$ http进行多次调用

使用Javascript多次调用REST API或具有大型响应正文的一次调用

根据服务器响应的变化进行ajax调用?

导航redux并基于API调用进行本地响应

处理用户输入以做出响应以进行API调用