首页 > 后端开发 > php教程 > IE 11下载文件中文乱码解决go/php

IE 11下载文件中文乱码解决go/php

WBOY
发布: 2016-08-08 09:22:42
原创
1112 人浏览过

IE 11下载文件的时候发现乱码.

网上查了下资料,了解了一下,主要是user-agent变了,不再是MSIE,面是rv:11.0这样,所以只要服务端判断出是IE 11,处理跟MSIE一样。

GO:

<span>func</span><span></span><span>setDownloadFileName</span><span>(</span>w<span></span>http<span>.</span>ResponseWriter<span>,</span><span></span>fileName<span>,</span><span></span>agent<span></span><span>string</span><span>)</span><span></span><span>{</span>
登录后复制
<span>    </span><span>if</span><span></span>strings<span>.</span>Contains<span>(</span>agent<span>,</span><span></span><span>"MSIE"</span><span>)</span><span></span><span>{</span>
登录后复制
<span>        </span>fileName<span></span><span>=</span><span></span>url<span>.</span>QueryEscape<span>(</span>fileName<span>)</span>
登录后复制
登录后复制
<span>        </span>fileName<span></span><span>=</span><span></span>strings<span>.</span>Replace<span>(</span>fileName<span>,</span><span></span><span>"+"</span><span>,</span><span></span><span>"%20"</span><span>,</span><span></span><span>-</span><span>1</span><span>)</span>
登录后复制
登录后复制
<span>    </span><span>}</span>
登录后复制
登录后复制
登录后复制
登录后复制
<span>    </span><span>if</span><span></span>strings<span>.</span>Contains<span>(</span>agent<span>,</span><span></span><span>"rv:"</span><span>)</span><span></span><span>&&</span><span></span>strings<span>.</span>Contains<span>(</span>agent<span>,</span><span></span><span>"Gecko"</span><span>)</span><span></span><span>{</span>
登录后复制
<span>        </span>fileName<span></span><span>=</span><span></span>url<span>.</span>QueryEscape<span>(</span>fileName<span>)</span>
登录后复制
登录后复制
<span>        </span>fileName<span></span><span>=</span><span></span>strings<span>.</span>Replace<span>(</span>fileName<span>,</span><span></span><span>"+"</span><span>,</span><span></span><span>"%20"</span><span>,</span><span></span><span>-</span><span>1</span><span>)</span>
登录后复制
登录后复制
<span>    </span><span>}</span>
登录后复制
登录后复制
登录后复制
登录后复制
<span>    </span>w<span>.</span>Header<span>().</span>Set<span>(</span><span>"Content-Disposition"</span><span>,</span><span></span><span>"attachment;filename=\""</span><span>+</span>fileName<span>+</span><span>"\""</span><span>)</span>
登录后复制
<span>}</span>
登录后复制

PHP:

$ua = isset ( $_SERVER ["HTTP_USER_AGENT"] ) ? $_SERVER ["HTTP_USER_AGENT"] : '';
<span style="white-space:pre">		</span>
if (preg_match ( "/MSIE/", $ua )) {
<span style="white-space:pre">	</span>$file_name = rawurlencode ( $file_name );
<span style="white-space:pre">	</span>header ( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
} else if (preg_match ( "/Firefox/", $ua )) {
<span style="white-space:pre">	</span>header ( 'Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '"' );
} elseif (stripos ( $ua, 'rv:' ) > 0 && stripos ( $ua, 'Gecko' ) > 0) {
<span style="white-space:pre">	</span>$file_name = rawurlencode ( $file_name );
<span style="white-space:pre">	</span>header ( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
} else {
<span style="white-space:pre">	</span>header ( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
}
登录后复制

以上就介绍了IE 11下载文件中文乱码解决go/php,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板