因为英语是我的母语,所以有些时候我也不知道我的web应用程序是否能和其它非美国的语言很好的兼容,这就是一个和“Content-Disposition”头相关的例子。我已经用了很多年了,但是,在上周我才发现Content-Disposition头中的“文件名”部分不自然的处理非美国的ASCII字符,幸运的是,现在的浏览器都支持对Content-Disposition头的拓展以允许UTF-8的编码字符串。
谷歌搜索了下,我发现了这个,其中有很多的关于Content-Disposition头的测试用例。这些测试表名,你可以加上一个特殊的符号。标准文件名加一个带URL-编码字符的UTF-8文件名是被允许的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<!–-
Query for the files in the directory.
–
NOTE: I am doing this so I don‘t have to embed high-ascii characters in the code – I don’t think my blog has the proper support for UTF-8
encoding? Not sure.
–->
<cfsetfiles=directoryList(expandPath(“./”),false,“name”,“Data*”)/>
<!–- Isolate the file with foreign characters. –->
<cfsetfileName=files[1]/>
<cfsetfilePath=expandPath(fileName)/>
<!–-
By default, the filename portion of the Content-Disposition header only allows for US-ASCII values. In order to account for foreign / exnteded ASCII values, we have to jump through some funky notation.
In this case, we are attempting to provide fallbacks. The first instance of “filename” is for browsers that do not support the RFC5987 encoding (they ignore the filename*= after the filename).
Then, for browsers that DO support the encoding, they will pick up the UTF-8 encoding.
Notice that the UTF-encoded value doesn‘t need to be quoted since the embeded spaces are url-encoded.
—>
<cfheader name=”content-disposition” value=”attachment; filename=”"#fileName#”"; filename*=UTF-8′‘#urlEncodedFormat( fileName )#” />
<cfcontent type=“text/plain; charset=utf-8″ file=“#filePath#” />
|
在这个例子中,”filename*=UTF-8””符号会被现在的浏览器保留,而老版浏览器则会将其忽略,直接返回“filename”。
当我运行所有的代码时,我被提示有一个文件下载:
请看,现在的编码已经是外国的字符(法国),这样,程序就可以在几乎所有现在的浏览器里正常工作了,包括最新版本的IE。
Copyright © hongdaChiaki. All Rights Reserved. 鸿大千秋 版权所有
联系方式:
地址: 深圳市南山区招商街道沿山社区沿山路43号创业壹号大楼A栋107室
邮箱:service@hongdaqianqiu.com
备案号:粤ICP备15078875号