Sunday, 15 November 2020

golang 18 http serve file

open download page by phone, available static files on server are displayed
press download icon

download shows progress


//main.go
package main

import (
"html/template"
"net/http"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"

"github.com/gorilla/mux"
)

var templates *template.Template
var downloadPath = rootDir() + "\\golang9\\download\\"

func indexGetHandler(w http.ResponseWriter, r *http.Request) {

templates.ExecuteTemplate(w, "index.html", nil)
}

func indexPostHandler(w http.ResponseWriter, r *http.Request) {

r.ParseForm()
filename := r.PostForm.Get("filename")
downloadHandler(w, r, filename)
}

func downloadHandler(w http.ResponseWriter, r *http.Request, filename string) {
w.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(filename))
w.Header().Set("Content-Type", "application/octet-stream")
path := downloadPath + filename
http.ServeFile(w, r, path)
}

func rootDir() string {
_, b, _, _ := runtime.Caller(0)
d := path.Join(path.Dir(b))
return filepath.Dir(d)
}

func open(url string) error {
var cmd string
var args []string

switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}

func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexGetHandler).Methods("GET")
r.HandleFunc("/", indexPostHandler).Methods("POST")
fs := http.FileServer(http.Dir("./static/"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
http.Handle("/", r)
//open("http://localhost:8000/")
http.ListenAndServe(":8000", nil)
}

--------------------------------
//index.html
<html>

<head>
    <title> Download</title>
    <link rel="stylesheet" type="text/css" href="/static/index.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>
    <div style="text-align: center;">
        <h1>Download file from server </h1>
    </div>

    <form method="POST">
        <input type="hidden" value="" id="input1" name="filename">
        <ul>
            <li>
                <h3>a.txt <i class="fa fa-download" onclick="download('a.txt')"></i></h3>
            </li>
            <li>
                <h3>w3school.jpg <i class="fa fa-download" onclick="download('w3school.jpg')"></i></h3>
            </li>
            <li>
                <h3>SSMS-Setup-ENU.exe <i class="fa fa-download" onclick="download('SSMS-Setup-ENU.exe')"></i></h3>
            </li>
        </ul>
        <button type="submit" id="button1" style="display: none;"></button>
    </form>


</body>
<script>
    function download(filename) {
        document.getElementById("input1").value = filename
        document.getElementById("button1").click()
    }
</script>

</html>
 
reference:

http.ServeFile

No comments:

Post a Comment