-
Notifications
You must be signed in to change notification settings - Fork 21
/
file-receive.go
85 lines (68 loc) · 1.77 KB
/
file-receive.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
//
// This example is an HTTP server that can receive a file upload.
// https://www.socketloop.com/tutorials/golang-upload-file
//
// To upload a file, go to :8080/upload
// To download file, go to :8080/
//
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
// infoHandler returns an HTML upload form
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
fmt.Fprintf(w, `<html>
<head>
<title>GoLang HTTP Fileserver</title>
</head>
<body>
<h2>Upload a file</h2>
<form action="/receive" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>`)
}
}
// receiveHandler accepts the file and saves it to the current working directory
func receiveHandler(w http.ResponseWriter, r *http.Request) {
// the FormFile function takes in the POST input id file
file, header, err := r.FormFile("file")
if err != nil {
fmt.Fprintln(w, err)
return
}
defer file.Close()
out, err := os.Create(header.Filename)
if err != nil {
fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
return
}
defer out.Close()
// write the content from POST to the file
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintf(w, "File uploaded successfully: ")
fmt.Fprintf(w, header.Filename)
}
func main() {
dir, err := os.Getwd()
if err != nil {
fmt.Println("err=", err)
os.Exit(1)
}
http.HandleFunc("/upload", uploadHandler) // Display a form for user to upload file
http.HandleFunc("/receive", receiveHandler) // Handle the incoming file
http.Handle("/", http.FileServer(http.Dir(dir)))
log.Fatal(http.ListenAndServe(":8080", nil))
}