dongyou6795
2016-12-14 14:08
浏览 356
已采纳

在Golang中从base64渲染图像

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?

图片转代码服务由CSDN问答提供 功能建议

我想在golang(此处为twitter图标)中从base64渲染图像 \ n

 包main 
 
import(
 base64“ encoding / base64” 
“ fmt” 
“ io” 
“ net / http” 
“ strconv” 
)
 \  nfunc pix(w http.ResponseWriter,r * http.Request){
 fmt.Fprintf(w,“嗨,我爱%s!”,r.URL.Path [1:])
 var cookie * http  .Cookie 
 Cookie,错误:= r.Cookie(“ csrftoken”)
,如果错误!= nil {
 fmt.Printf(“ error”)
 fmt.Println(err)
} 
 fmt。 的printf(cookie.Value)
 w.Header()设置( “内容类型”, “图像/ JPEG”)\ NP,ERR:= base64.StdEncoding.DecodeString(“iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR / mAAADMUlEQVRYw + 2YTUgUYRjHZzOJIoNA + rrUyYNIRQgRHaLo4qFDBEGeunSxS9TFU0QEnhIh6IvokrUzO2uamRmbG6XmR / mVaKZpZVbYvvO143zszsxOz  + yahNm + 785sITEP72F3Z + ADH8 / ZF5 + PpagwtxKXj + VJ + Vg的+ LO / LY + W + WI4KpddKwWIQFUSF97nNLcLGZt75SiOHchEXfskDVmYjlowpiEoei3UT2ljcFJOpOd169C1Z2SuvgsdpB7cgzB16EV / byGM2xDIVPxQujKmBDF / 2m2l0vFvmEin7N2v8kiiP  iOeGlGHRvP1RdxA9eYtGR7pk2Pf6lI7RCoP2RaWkZWe3fsFc18hvesAHPGEFUc24ltnx3kyiCJwfRMs6dTXLdSIjO9Osal18qzKfE5V9coDxhlU7qS3uOyiaB55JDtkS2TKoLCLaOLPS4b02pQdCHiUfRKf653 / d2kjZN6f10jYxI2EnrGk5H + 2WsVi6ZZ8fVSmGQKaYyyFuR6ugmUtVrJo2C7HokeGq8447sYpOPBbo3XFzKC95626sZlz905sUM9XLGbXvtKtTOhZrQDApkhNNkiAOPo / viojh2YSZsj1aF2eQ5n2stuomNQjiiGQanrFufdCXP8gu8tbhjridJ6saVPKExXJrwlwfb3pnAg2Ut0tEBZFI8gza81Tik15DCDIoINQ7aQdBo90RMfrdwNaWLFY9opJGkBQrhCA / HXspQ8W1XHkN6vfWFiGH9ouwhdpJUFuy2JX3eg6uyqENpNHZYcUd02jcLMI2WO67UwZVv1G1HLMq3L83KuEbLPdY7IL2L42p0MMQiuzkq / ncwucOi6qPbWkWoPfCUsENpweUnP1EmE4XGhgagT72RyXolkSCHBbTU3By3fgJj8VyJW3CmSHl8oTWMJuYUUizVvtcsuyJ6J4J663CMLevXar / lJgnKNSgbphzKjriTn5i0F8eX9ODXnEzf6JHvjGtv + aNGdWCOEKnJRmpr5oFVQV8WTWglIKHMlPhv5uqQ1xGYfB5fRMPo + n2VmFbi7ChiS9oWBhZvXrI01TNLg7yPxt51v9rxMfysXwsH8vH + G + wfgDUr + 5LcyNV4AAAAABJRU5ErkJggg ==“)
如果犯错!=零{
 http.Error(W, ”内部错误“,500)  
 return 
} 
 w.Header()。Set(“ Content-Length”,strconv.Itoa(len(p)))// len(dec)
 io.WriteStr  ing(w,string(p))
} 
 
func main(){
 http.HandleFunc(“ / pix /”,pix)
 err:= http.ListenAndServe(“:9080”,nil)  
 if err!= nil {
 fmt.Println(err)
} 
} 
   
 
 

但是它什么也不显示,当我尝试时 转到URL; 它会下载包含错误的文件。 有人知道为什么会这样吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudu161481 2016-12-14 14:17
    已采纳

    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)).

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题