forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (34 loc) · 793 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"io"
"net/http"
"os"
"path/filepath"
)
func main() {
http.ListenAndServe(":9000", http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
file, _, err := req.FormFile("my-file")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer file.Close()
src := io.LimitReader(file, 400)
dst, err := os.Create(filepath.Join(".", "file.txt"))
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer dst.Close()
io.Copy(dst, src)
}
res.Header().Set("Content-Type", "text/html")
io.WriteString(res, `
<form method="POST" enctype="multipart/form-data">
<input type="file" name="my-file">
<input type="submit">
</form>
`)
}))
}