-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileUploader.go
108 lines (94 loc) · 2.71 KB
/
fileUploader.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", homePage)
http.HandleFunc("/upload", uploadFile)
http.HandleFunc("/upload-multiple", uploadMultipleFiles)
fmt.Println("Server started at http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
func homePage(w http.ResponseWriter, r *http.Request) {
html := `<html>
<head>
<title>File Upload</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="myFile" />
<input type="submit" value="Upload Single File" />
</form>
<form enctype="multipart/form-data" action="/upload-multiple" method="post">
<input type="file" name="myFiles" multiple />
<input type="submit" value="Upload Multiple Files" />
</form>
</body>
</html>`
fmt.Fprint(w, html)
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// Parse the form data
r.ParseMultipartForm(10 << 20) // 10 MB max memory
// Retrieve the file from form data
file, handler, err := r.FormFile("myFile")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
defer file.Close()
// Create a new file in the server's local storage
dst, err := os.Create(handler.Filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
// Copy the uploaded file to the filesystem at the specified destination
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Successfully Uploaded File\n")
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}
func uploadMultipleFiles(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// Parse the form data
r.ParseMultipartForm(10 << 20) // 10 MB max memory
// Retrieve the file form data
files := r.MultipartForm.File["myFiles"]
for _, fileHeader := range files {
// Open the file
file, err := fileHeader.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
// Create a new file in the server's local storage
dst, err := os.Create(fileHeader.Filename)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
// Copy the uploaded file to the filesystem at the specified destination
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
fmt.Fprintf(w, "Successfully Uploaded Multiple Files\n")
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}