template_controller.go 1.5 KB
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)
}