Added basic functionality

This commit is contained in:
raul 2024-04-30 11:25:36 +02:00
parent bc91175e1c
commit 7ac8586715
3 changed files with 24 additions and 6 deletions

View File

@ -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
View File

@ -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)

2
temp/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore