ブラウザアクセスでファイルをダウンロードさせる方法

やり方

HTTPレスポンスヘッダーにContent-Disposition: attachmentをセットする。
デフォルトでは、Content-Disposition: inlineとなっており、Webページとして表示される。 ちなみにdispositionの意味は、何かの置き方や配置の仕方、という意味。

 The way in which something is placed or arranged, especially in relation to other things

サンプル

例えばgoで書いてみるとこんな感じ。
localhost:8080 にアクセスするとファイルが素敵なお手紙がダウンロードされる。

package main

import (
    "bytes"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    w.Header().Set("Content-Disposition", "attachment; filename=nekootoko3.txt")

    var buf bytes.Buffer
    buf.WriteString("Hello!\n")
    buf.WriteString("I'm nekootoko3\n")
    buf.WriteString("I'm happay everyday!\n")
    buf.WriteString("\n")
    buf.WriteString("Sincerely Yours\n")
    buf.WriteString("nekootoko3")
    txt := buf.Bytes()

    w.Write(txt)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

参考