I would like to query data from database and mapping data then streaming CSV file via http. How can I do like that? I try like this but I got filename “export” into directory. Any idea?
func (resource Resource) export(c echo.Context) error {
c.Response().Header().Set("Content-Type", "application/octet-stream")
c.Response().Header().Set("Content-Disposition", "attachment; filename="+"my_result.csv")
c.Response().Header().Set("Content-Transfer-Encoding", "binary")
c.Response().Header().Set("Expires", "0")
c.Response().WriteHeader(http.StatusOK)
var w *csv.Writer
for _, record := range records {
w = csv.NewWriter(c.Response())
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
w.Flush()
c.Response().Flush()
time.Sleep(1 * time.Second)
}
return nil
}