티스토리 뷰

MidJourney: embed files to the binary, programmger, gopher, Realistic, NostelgicCore

개요

1. 특정 파일 하나를 embed 하여 고객이 template으로 요구할 경우 다운로드 할 수 있도록 하고 싶다.

2. Golang echo framework 를 사용중이다.

내용

GitHub: https://github.com/nicewook/echo-serve-embedfile

  1. embed로 가져온 다음
  2. ReadFile() 메서드로 파일을 읽어 바이트 슬라이스로 만든 다음
  3. Content Disposition 헤더로 파일이름을 지정하고
  4. ContentType을 명시한 다음 바이트 슬라이스를 회신한다.
//go:embed static
var static embed.FS

func main() {

	e := echo.New()

	e.GET("/csv", func(c echo.Context) error {
		p, err := static.ReadFile("static/userinfo.csv")
		if err != nil {
			log.Println(err)
			return err
		}
		c.Response().Header().Set(echo.HeaderContentDisposition, "attatchment; filename=userinfo.csv")
		return c.Blob(http.StatusOK, "text/csv", p)
	})

	e.Logger.Fatal(e.Start(":1323"))
}
반응형
반응형
잡학툰 뱃지
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함