IE 11 未正确解析 HTTP 内容处置 header
IE 11 doesn't parse HTTP Content Disposition header correctly
我正在尝试设置内容处置 header 以在 IE 11 中执行文件下载时获取自定义文件名。
我在 IE 11 中遇到了极其奇怪的行为:当转义 (U+001B) 字符出现在内容处置的文件名中时 header IE 11 无法设置正确的文件名(在内容处置中提供 Header).例如 content_disposition
与格式化 file_name_1
触发 IE 11 错误,但格式化与 file_name_2
没问题:
# There are invisible control character escape (U+001B)
name = u'_$B!'
file_name_1 = quote(name) # gives "_%1B%24B%21"
file_name_2 = "_%24B%21.pdf"
content_disposition = "attachment; filename*=utf-8''%s" % file_name_2
有什么方法可以在 IE11 中提供对任何 utf8 文件名的支持。
注意:在所有其他浏览器中,一切似乎都运行良好,我希望有办法在不破坏其他浏览器的情况下修复 IE 11 行为...
不接受这样的字符实际上是规范的建议。见 https://greenbytes.de/tech/webdav/rfc6266.html#rfc.section.4.3.p.5:
"Recipients SHOULD strip or replace character sequences that are known to cause confusion both in user interfaces and in filenames, such as control characters and leading and trailing whitespace."
当然有人会争论保留剩余字符会更好,但重要的一点是建议用户代理去除控制字符。
建议在形成内容配置之前去除控制字符header(@JulianReschke 回答中的更多信息)。
Control codes in python may be stripped easily with list comprehension and ord:
# There are invisible control character escape (U+001B)
name = u'_$B!'
# All chars with ord(s) <= 31 is a control characters.
name = "".join(i for i in name if ord(i) > 31)
所以在代码中添加上面的行后,一切开始正常工作:
name = u'_$B!'
# Strip control codes
name = "".join(i for i in name if ord(i) > 31)
file_name_1 = quote(name) # gives "_%1B%24B%21"
file_name_2 = "_%24B%21.pdf"
content_disposition = "attachment; filename*=utf-8''%s" % file_name_2
我正在尝试设置内容处置 header 以在 IE 11 中执行文件下载时获取自定义文件名。
我在 IE 11 中遇到了极其奇怪的行为:当转义 (U+001B) 字符出现在内容处置的文件名中时 header IE 11 无法设置正确的文件名(在内容处置中提供 Header).例如 content_disposition
与格式化 file_name_1
触发 IE 11 错误,但格式化与 file_name_2
没问题:
# There are invisible control character escape (U+001B)
name = u'_$B!'
file_name_1 = quote(name) # gives "_%1B%24B%21"
file_name_2 = "_%24B%21.pdf"
content_disposition = "attachment; filename*=utf-8''%s" % file_name_2
有什么方法可以在 IE11 中提供对任何 utf8 文件名的支持。
注意:在所有其他浏览器中,一切似乎都运行良好,我希望有办法在不破坏其他浏览器的情况下修复 IE 11 行为...
不接受这样的字符实际上是规范的建议。见 https://greenbytes.de/tech/webdav/rfc6266.html#rfc.section.4.3.p.5:
"Recipients SHOULD strip or replace character sequences that are known to cause confusion both in user interfaces and in filenames, such as control characters and leading and trailing whitespace."
当然有人会争论保留剩余字符会更好,但重要的一点是建议用户代理去除控制字符。
建议在形成内容配置之前去除控制字符header(@JulianReschke 回答中的更多信息)。
Control codes in python may be stripped easily with list comprehension and ord:
# There are invisible control character escape (U+001B)
name = u'_$B!'
# All chars with ord(s) <= 31 is a control characters.
name = "".join(i for i in name if ord(i) > 31)
所以在代码中添加上面的行后,一切开始正常工作:
name = u'_$B!'
# Strip control codes
name = "".join(i for i in name if ord(i) > 31)
file_name_1 = quote(name) # gives "_%1B%24B%21"
file_name_2 = "_%24B%21.pdf"
content_disposition = "attachment; filename*=utf-8''%s" % file_name_2