我想获取 io.reader 的大小以返回图片的长度以解决 bse64 问题。
主要问题是获取 dec 的大小以返回内容长度标头
这是我的代码:
package main
import (
"fmt"
"net/http"
"time"
"strconv"
"io"
base64 "encoding/base64"
"bytes"
)
func pix(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
cookie, _ := r.Cookie("csrftoken")
fmt.Printf(cookie.Value)
}
func img(w http.ResponseWriter, r *http.Request) {
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "csrftoken",Value:"abcd",Expires:expiration,HttpOnly: false}
http.SetCookie(w, &cookie)
dec := base64.NewDecoder(base64.StdEncoding, img)
w.Header().Set("Content-Type", "image/jpeg")
// the problem is here i would like to get the size to return the length of the picture (lend(dec))
w.Header().Set("Content-Length", strconv.Itoa(len(dec)))
io.Copy(w, dec)
}
侃侃无极
冉冉说
HUWWW
相关分类