Added basic functionality
This commit is contained in:
parent
bc91175e1c
commit
7ac8586715
10
index.html
10
index.html
|
@ -5,11 +5,11 @@
|
|||
</head>
|
||||
<body>
|
||||
<p><b>Test</b></p>
|
||||
<!-- <form action="/action_page.php"> -->
|
||||
<!-- <input type="file" id="myFile" name="filename"> -->
|
||||
<!-- <input type="submit"> -->
|
||||
<form>
|
||||
|
||||
<form id='form' hx-encoding='multipart/form-data' hx-post='/api/upload'>
|
||||
<input type="file" name="file" value="">
|
||||
<button>
|
||||
Upload
|
||||
</button>
|
||||
</form>
|
||||
</form>
|
||||
</body>
|
||||
|
|
18
main.go
18
main.go
|
@ -24,13 +24,29 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func uploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("Received upload!\n")
|
||||
fmt.Println(w, "You have uploaded something")
|
||||
fmt.Fprintln(w, "You have uploaded something")
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error happened receiving file: %v\n", err)
|
||||
}
|
||||
fmt.Fprintf(w, "%v", header.Header)
|
||||
f, err := os.OpenFile("./temp/"+header.Filename, os.O_WRONLY|os.O_CREATE, 0660)
|
||||
if err != nil {
|
||||
log.Printf("Error happened opening file: %v\n", err)
|
||||
}
|
||||
io.Copy(f, file)
|
||||
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", getRoot)
|
||||
http.HandleFunc("/api/upload", uploadFile)
|
||||
|
||||
fmt.Println("Listening on port 8080...")
|
||||
|
||||
err := http.ListenAndServe(":8080", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
Loading…
Reference in New Issue