Added basic functionality
This commit is contained in:
parent
bc91175e1c
commit
7ac8586715
10
index.html
10
index.html
|
@ -5,11 +5,11 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<p><b>Test</b></p>
|
<p><b>Test</b></p>
|
||||||
<!-- <form action="/action_page.php"> -->
|
<form id='form' hx-encoding='multipart/form-data' hx-post='/api/upload'>
|
||||||
<!-- <input type="file" id="myFile" name="filename"> -->
|
<input type="file" name="file" value="">
|
||||||
<!-- <input type="submit"> -->
|
<button>
|
||||||
<form>
|
Upload
|
||||||
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</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) {
|
func uploadFile(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Printf("Received upload!\n")
|
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() {
|
func main() {
|
||||||
http.HandleFunc("/", getRoot)
|
http.HandleFunc("/", getRoot)
|
||||||
http.HandleFunc("/api/upload", uploadFile)
|
http.HandleFunc("/api/upload", uploadFile)
|
||||||
|
|
||||||
|
fmt.Println("Listening on port 8080...")
|
||||||
|
|
||||||
err := http.ListenAndServe(":8080", nil)
|
err := http.ListenAndServe(":8080", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
*
|
||||||
|
!.gitignore
|
Loading…
Reference in New Issue