go:代码

import (
 "net/http"
 "encoding/json"
)

func main() {
 http.HandleFunc("/down",downloadFile)
 http.ListenAndServe("127.0.0.1:8080",nil)
}
type User struct {
 Name string `json:"name"`
 Id   int64  `json:"id"`
}


func downloadFile(res http.ResponseWriter, req *http.Request) {
 user := User{
  Name : "admin",
  Id : 1,
 }

 userJson,_ := json.Marshal(user)
 userString := string(userJson)
 d := []byte(userString)


 res.Header().Add("content-type","application/octet-stream;charset=utf-8")
 res.Header().Add("content-Disposition","filename=fileName")
 res.Write([]byte("this is title"))
 res.Write(d)
}

有疑问加站长微信联系(非本文作者)