diff options
author | Matthew Lemon <y@yulqen.org> | 2024-03-12 09:07:20 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-03-12 09:07:20 +0000 |
commit | f3292c0e761b46b589cd0a3bb054a1493e99c6d8 (patch) | |
tree | 03b41ef2d478ae6ddd09423a6a8ec545715684c4 | |
parent | b88dc4c9c5e6b37fcc6ac8b55761dcf1351bca9c (diff) |
First implementation of csv upload
-rw-r--r-- | cmd/api/datamaps.go | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/cmd/api/datamaps.go b/cmd/api/datamaps.go index 517d82d..6bbda2b 100644 --- a/cmd/api/datamaps.go +++ b/cmd/api/datamaps.go @@ -2,12 +2,44 @@ package main import ( "fmt" + "io" "net/http" + "os" "strconv" ) func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, "create new datamaps page") + // Parse the multipart form + err := r.ParseMultipartForm(10 << 20) // 10Mb max + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + // get the uploaded file + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "Missing file", http.StatusBadRequest) + return + } + defer file.Close() + + // create a new file on the server + outFile, err := os.Create("uploaded.csv") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer outFile.Close() + + // copy the uploaded file to the server file + _, err = io.Copy(outFile, file) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + fmt.Fprintf(w, "File uploaded successfully") } func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Request) { |