美文网首页
golang文件下载

golang文件下载

作者: 叫我小码哥 | 来源:发表于2020-07-20 19:37 被阅读0次

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

相关文章

网友评论

      本文标题:golang文件下载

      本文链接:https://www.haomeiwen.com/subject/gdoykktx.html