From f3292c0e761b46b589cd0a3bb054a1493e99c6d8 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Tue, 12 Mar 2024 09:07:20 +0000 Subject: First implementation of csv upload --- cmd/api/datamaps.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'cmd/api') 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) { -- cgit v1.2.3