[feat] FileResponse 支持 header 头定义
This commit is contained in:
parent
beeb0840d5
commit
e434c33afb
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user