Output to csv file dumps the data to browser but not dump to file

前端 未结 1 428
梦毁少年i
梦毁少年i 2021-01-21 01:04

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{
            


        
相关标签:
1条回答
  • 2021-01-21 01:18

    You should always set response headers first and only after that start writing any data to the output. I know you called writer.Flush() after setting header fields, but that is no guarantee whatsoever that data will not be flushed or sent before that - which will imply default headers being sent. After this no additional headers can be sent or changed.

    Also the proper mime type for CSV is text/csv not application/csv (rfc4180).

    Also the headers are more like a "proposal" to the browser. That's one thing you suggest the response is a file to be saved, but from the server side you can't force the browser to really save the response to a file and not display it.

    See rfc1806, rfc2183 and rfc6266 for more details about the "Content-Disposition" header field. Basically it communicates presentation information.

    The Content-Disposition response header field is used to convey additional information about how to process the response payload, and also can be used to attach additional metadata, such as the filename to use when saving the response payload locally.

    0 讨论(0)
提交回复
热议问题