aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-03-12 09:25:35 +0000
committerMatthew Lemon <y@yulqen.org>2024-03-12 09:25:35 +0000
commit324ee663d30d4529e91815f076aca53f8945a13c (patch)
tree2f431e6ccc3b599ede16620b220e0131ba8782f8
parentf3292c0e761b46b589cd0a3bb054a1493e99c6d8 (diff)
wip: writes contents of uploaded csv file to http.ResponseWriter
-rw-r--r--cmd/api/datamaps.go40
1 files changed, 28 insertions, 12 deletions
diff --git a/cmd/api/datamaps.go b/cmd/api/datamaps.go
index 6bbda2b..0d769e4 100644
--- a/cmd/api/datamaps.go
+++ b/cmd/api/datamaps.go
@@ -4,7 +4,6 @@ import (
"fmt"
"io"
"net/http"
- "os"
"strconv"
)
@@ -24,22 +23,39 @@ func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Requ
}
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()
+ // // create a new file on the server
+ // outFile, err := os.CreateTemp("", "uploaded_csv")
+ // if err != nil {
+ // http.Error(w, err.Error(), http.StatusInternalServerError)
+ // return
+ // }
+ // // clean up - we have to do this
+ // defer os.Remove(outFile.Name())
- // copy the uploaded file to the server file
- _, err = io.Copy(outFile, file)
+ // // copy the uploaded file to the server file
+ // _, err = io.Copy(outFile, file)
+ // if err != nil {
+ // http.Error(w, err.Error(), http.StatusInternalServerError)
+ // return
+ // }
+
+ // var b []byte // this doesn't work
+ // _, err = outFile.Read(b)
+ // if err != nil {
+ // http.Error(w, err.Error(), http.StatusInternalServerError)
+ // return
+ // }
+ // fmt.Fprintf(w, string(b))
+
+ // Read the contents of the file
+ fileBytes, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
-
- fmt.Fprintf(w, "File uploaded successfully")
+ // Write the file contents to the response
+ w.Header().Set("Content-Type", "text/csv") // Set the appropriate content type
+ w.Write(fileBytes)
}
func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Request) {