LoginSignup
11
12

More than 5 years have passed since last update.

go言語でのファイルダウンロードリンクの作り方(goji

Posted at

内部で作ったCSV形式の文字列をファイルとしてダウンロードさせたかったのだが、いい感じの説明が見当たらなかったのでメモ

sample.go
func (self *Sample) OutputCSV(c web.C, w http.ResponseWriter, r *http.Request) {
    // 保存させたい文字列をbyte配列にする
    out := []byte("test,test2,test3")

    // いろんな文字列情報
    // ファイル名
    w.Header().Set("Content-Disposition", "attachment; filename=test.csv")
    // コンテントタイプ
    w.Header().Set("Content-Type", "text/csv")
    // ファイルの長さ
    w.Header().Set("Content-Length", string(len(out)))
    // bodyに書き込み
    w.Write(out)
}

こちらを参考にしました
golang - how to download file in browser from golang server?

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12