feat attachment filename support utf8 (#3071)

This commit is contained in:
metal A-wing
2022-03-17 03:56:16 +00:00
committed by GitHub
parent 90330e2a76
commit 8860527de6
4 changed files with 35 additions and 2 deletions

View File

@ -6,7 +6,6 @@ package gin
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
@ -1018,7 +1017,11 @@ func (c *Context) FileFromFS(filepath string, fs http.FileSystem) {
// FileAttachment writes the specified file into the body stream in an efficient way
// On the client side, the file will typically be downloaded with the given filename
func (c *Context) FileAttachment(filepath, filename string) {
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
if isASCII(filename) {
c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)
} else {
c.Writer.Header().Set("Content-Disposition", `attachment; filename*=UTF-8''`+url.QueryEscape(filename))
}
http.ServeFile(c.Writer, c.Request, filepath)
}