这是我的代码,它使用excelize包创建一个新的xlsx文件,并将其作为字节流返回。在localhost上一切正常,但当我将其部署到vps时,文件已损坏。
func exportdata(rw http.ResponseWriter, request *http.Request) {
xlsx := excelize.NewFile()
xlsx.SetCellValue("Sheet1", "A1", "a")
var b bytes.Buffer
writr := bufio.NewWriter(&b)
xlsx.Write(writr)
writr.Flush()
fileContents := b.Bytes()
fileSize := strconv.Itoa(len(fileContents))
rw.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
rw.Header().Set("Content-disposition", "attachment;filename=export.xlsx")
rw.Header().Set("Content-Length", fileSize)
rw.Write(b.Bytes())
// t := bytes.NewReader(b.Bytes())
// http.ServeContent(rw, request, "export.xlsx", time.Now(), t)
}
func main() {
http.HandleFunc("/", exportdata)
http.ListenAndServe(":8080", nil)
}