티스토리 뷰
개요
1. 특정 파일 하나를 embed 하여 고객이 template으로 요구할 경우 다운로드 할 수 있도록 하고 싶다.
2. Golang echo framework 를 사용중이다.
내용
GitHub: https://github.com/nicewook/echo-serve-embedfile
- embed로 가져온 다음
- ReadFile() 메서드로 파일을 읽어 바이트 슬라이스로 만든 다음
- Content Disposition 헤더로 파일이름을 지정하고
- 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"))
}
반응형
'golang' 카테고리의 다른 글
Golang: HTTP 클라이언트의 연결 관리(2) (0) | 2023.05.14 |
---|---|
Golang: HTTP 클라이언트의 연결 관리(1) (0) | 2023.05.14 |
Golang ORM - Bun 삽질 이야기 - timestamp 와 timestamptz (0) | 2022.12.18 |
Golang: go get으로 패키지를 최신으로 업데이트 하는 법 (0) | 2022.12.15 |
Golang ORM - Bun 삽질 이야기 - Automatic timestamp (0) | 2022.12.10 |
반응형

최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 2023
- 티스토리챌린지
- OpenAI
- 엉클 밥
- 2024년
- ChatGPT
- API
- 클린 아키텍처
- go
- Bug
- 인텔리제이
- clean agile
- 독서
- websocket
- intellij
- strange
- golang
- Gin
- bun
- 클린 애자일
- 영화
- 독서후기
- 노션
- 잡학툰
- notion
- backend
- solid
- agile
- 오블완
- postgres
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함