From e434c33afbe01c4572418957b6a227f1284a147b Mon Sep 17 00:00:00 2001 From: what Date: Fri, 22 Nov 2024 14:54:53 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20FileResponse=20=E6=94=AF=E6=8C=81=20he?= =?UTF-8?q?ader=20=E5=A4=B4=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- support/response.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/support/response.go b/support/response.go index ae93704..644ab2d 100644 --- a/support/response.go +++ b/support/response.go @@ -24,6 +24,7 @@ type RawResponse struct { } type FileResponse struct { + headers [][2]string disposition string name string } @@ -78,12 +79,17 @@ func (this FileResponse) Get(path ...string) req.GlobalParams { } func (this FileResponse) Send(w http.ResponseWriter, r *http.Request) { - if this.Get("disposition").String() != "" { - w.Header().Set("Content-Disposition", `attachment; filename="`+this.Get("disposition").String()+`"`) - } if _, err := os.Stat(this.Get("name").String()); err != nil && os.IsNotExist(err) { w.WriteHeader(http.StatusNotFound) } else { + if this.Get("disposition").String() != "" { + w.Header().Set("Content-Disposition", `attachment; filename="`+this.Get("disposition").String()+`"`) + } + + for i := 0; i < len(this.headers); i++ { + w.Header().Set(this.headers[i][0], this.headers[i][1]) + } + http.ServeFile(w, r, this.Get("name").String()) } } @@ -96,8 +102,8 @@ func NewRawResponse(b []byte, headers ...[2]string) req.HttpResponse { return &RawResponse{raw: b, headers: headers} } -func NewFileResponse(name string, disposition string) req.HttpResponse { - return &FileResponse{name: name, disposition: disposition} +func NewFileResponse(name string, disposition string, headers ...[2]string) req.HttpResponse { + return &FileResponse{name: name, disposition: disposition, headers: headers} } func NewJsonResponse(b []byte) req.HttpResponse {