I tried to ouput some data to csv file in golang beego framework, here is my code
records := make([][]string,len(devicesData))
for k,v := range devicesData{
records[k] = []string{v.Fields.Country,v.Fields.Imei[0],v.Fields.Number[0]}
}
writer := csv.NewWriter(this.Controller.Ctx.ResponseWriter)
for _, record := range records {
err := writer.Write(record)
if err != nil {
fmt.Println("Error:", err)
return
}
}
this.Ctx.Output.Header("Content-Type", "application/csv")
this.Ctx.Output.Header("Content-Disposition", "attachment; filename=MyVerySpecial.csv")
writer.Flush()
However, it only shows the record data in browser, it cannot download the file. I have some loging controller and filter function before this download file controller, I do not whether it is affected. What's wrong with my code? Thanks