我想请教一下怎样 ==不保存== 生成的文件而直接返回给浏览器
```
func (this *MyHandler)DownloadHandler(w http.ResponseWriter, r *http.Request) () {
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
row.SetHeightCM(1) //设置每行的高度
cell := row.AddCell()
cell.Value = "haha"
cell = row.AddCell()
cell.Value = "xixi"
// ==================================
err := file.Save("./files/file.xlsx")
if err != nil {
panic(err)
}
w.Header().Set("Content-Disposition", "attachment; filename=file.xls")
//io.Copy(w, f)
http.ServeFile(w, r, "./files/haha2.xls")
return
}
```
我目前这种实现方式服务端必须先保存到服务器,然后再通过文件服务返回给浏览器
```
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
row.SetHeightCM(1) //设置每行的高度
cell := row.AddCell()
cell.Value = "haha"
cell = row.AddCell()
cell.Value = "xixi"
w.Header().Set("Content-Disposition", "attachment; filename=file.xls")
ret, _ := json.Marshal(file)
w.Write(ret)
```
我这样写会报栈溢出
#2
更多评论