Maison > Article > développement back-end > IE 11下载文件中文乱码解决go/php
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教程有兴趣的朋友有所帮助。