审查视图

pkg/port/beego/controllers/template_controller.go 1.9 KB
郑周 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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")
}
郑周 authored
23 24 25 26 27 28 29 30 31
func (controller *TemplateImplController) TemplateParentUser() {
	fileBytes, err := ioutil.ReadFile("./templates/tpl_template_user.xlsx")
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未读取到模板文件"))
		return
	}
	controller.WriteExcel(fileBytes, "直接上级导入模板.xlsx")
}
郑周 authored
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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)
}