From 165f4110832945e99e481920ab96cdcc304ffbd0 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 5 Jan 2015 19:46:02 -0600 Subject: [PATCH] Add filename when exporting results as csv, fixes #75 --- api.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/api.go b/api.go index b1c4e68..397d820 100644 --- a/api.go +++ b/api.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/gin-gonic/gin" ) @@ -292,11 +293,11 @@ func API_HandleQuery(query string, c *gin.Context) { q := c.Request.URL.Query() - if len(q["format"]) > 0 { - if q["format"][0] == "csv" { - c.Data(200, "text/csv", result.CSV()) - return - } + if len(q["format"]) > 0 && q["format"][0] == "csv" { + filename := fmt.Sprintf("pgweb-%v.csv", time.Now().Unix()) + c.Writer.Header().Set("Content-disposition", "attachment;filename="+filename) + c.Data(200, "text/csv", result.CSV()) + return } c.JSON(200, result)