不同浏览器使用Contentdisposition时filename带空格的处理方式不同

    最近在做项目中遇到一个问题,纠结了好久才找到原因。起因:通过MIME的扩展Content-disposition来实现在客户端保存附加文件(快捷方式)。问题:在chrome和IE8+下一切都很和谐,浏览器会弹出保存文件的提示,保存名字和类型都对;但在一向很标准的firefox下文件名保存成了第一个空格之前的单词,文件类型也变成了application/octet-stream,基本上没有文件可以打开这个无后缀名的文件。

(IE11)

(firefox)

(chrome)

    查了半天终于发现原来是后台对于response header里面的filename没有双引号包起来,而是直接Content-Disposition:attachment; filename=Super Mario Flash 2.0.url。这样的话在firefox中就会把文件名解析成Super,后缀名也没了。解决方法也很简单,后台写header的时候把filename用双引号包起来就可以了:Content-Disposition:attachment; filename="Super Mario Flash 2.0.url"。另一方面,如果用双引号把文件名包起来,对于保存文件时空格解析成%20的问题也会很大程度上避免了。

参考文章:

1、Filenames with spaces are truncated upon download

2、content-disposition filename cut (truncated) at space

3、Content-Disposition filename with space and without extension is not decoded.

4、content-disposition attachment filename 在Firefox和IE中得到不同的结果

原文地址:https://www.cnblogs.com/shinnyChen/p/3722329.html