template_controller.go
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package controllers
import (
"io/ioutil"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego"
)
type TemplateImplController struct {
beego.BaseController
}
func (controller *TemplateImplController) TemplateQuestion() {
fileBytes, err := ioutil.ReadFile("./templates/tpl_template_question.xlsx")
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未读取到模板文件"))
return
}
controller.WriteExcel(fileBytes, "评估导入模板.xlsx")
}
func (controller *TemplateImplController) WriteExcel(fileBytes []byte, fileName string) {
controller.Ctx.Output.Header("Content-Disposition", "attachment;filename="+fileName)
controller.Ctx.Output.Header("Content-Description", "FileTransfer")
controller.Ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
controller.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
controller.Ctx.Output.Header("Expires", "0")
_, _ = controller.Ctx.ResponseWriter.Write(fileBytes)
}
func (controller *TemplateImplController) WriteBinary(fileBytes []byte, fileName string) {
controller.Ctx.Output.Header("Content-Disposition", "attachment;filename="+fileName)
controller.Ctx.Output.Header("Content-Description", "FileTransfer")
controller.Ctx.Output.Header("Content-Type", "application/octet-stream")
controller.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
controller.Ctx.Output.Header("Expires", "0")
_, _ = controller.Ctx.ResponseWriter.Write(fileBytes)
}