From a9ffc2a0783b6f8838028073c781a4b5fa56e04a Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 23 Jan 2019 12:30:57 +0800 Subject: [PATCH 1/2] https://github.com/astaxie/beego/issues/3446 Use UTF-8 as the encoding of the "filename*" parameter, when present, because at least one existing implementation only implements that encoding. --- context/output.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/context/output.go b/context/output.go index 3e277ab2..8a1a1628 100644 --- a/context/output.go +++ b/context/output.go @@ -30,7 +30,8 @@ import ( "strconv" "strings" "time" - "gopkg.in/yaml.v2" + + yaml "gopkg.in/yaml.v2" ) // BeegoOutput does work for sending response header. @@ -203,7 +204,6 @@ func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, encoding bool) return output.Body(content) } - // YAML writes yaml to response body. func (output *BeegoOutput) YAML(data interface{}) error { output.Header("Content-Type", "application/x-yaml; charset=utf-8") @@ -288,7 +288,19 @@ func (output *BeegoOutput) Download(file string, filename ...string) { } else { fName = filepath.Base(file) } - output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fName)) + //https://tools.ietf.org/html/rfc6266#section-4.3 + fn := url.PathEscape(fName) + if fName == fn { + output.Header("Content-Disposition", "attachment; filename="+fn) + } else { + /** + The parameters "filename" and "filename*" differ only in that + "filename*" uses the encoding defined in [RFC5987], allowing the use + of characters not present in the ISO-8859-1 character set + ([ISO-8859-1]). + */ + output.Header("Content-Disposition", "attachment; filename*=utf-8''"+fn) + } output.Header("Content-Description", "File Transfer") output.Header("Content-Type", "application/octet-stream") output.Header("Content-Transfer-Encoding", "binary") From 1c893996c0af969d529274ecacaf0eb21fef424b Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 23 Jan 2019 12:36:14 +0800 Subject: [PATCH 2/2] improve the download func code --- context/output.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/context/output.go b/context/output.go index 8a1a1628..238dcf45 100644 --- a/context/output.go +++ b/context/output.go @@ -291,7 +291,7 @@ func (output *BeegoOutput) Download(file string, filename ...string) { //https://tools.ietf.org/html/rfc6266#section-4.3 fn := url.PathEscape(fName) if fName == fn { - output.Header("Content-Disposition", "attachment; filename="+fn) + fn = "filename=" + fn } else { /** The parameters "filename" and "filename*" differ only in that @@ -299,8 +299,9 @@ func (output *BeegoOutput) Download(file string, filename ...string) { of characters not present in the ISO-8859-1 character set ([ISO-8859-1]). */ - output.Header("Content-Disposition", "attachment; filename*=utf-8''"+fn) + fn = "filename=" + fName + "; filename*=utf-8''" + fn } + output.Header("Content-Disposition", "attachment; "+fn) output.Header("Content-Description", "File Transfer") output.Header("Content-Type", "application/octet-stream") output.Header("Content-Transfer-Encoding", "binary")