首頁 > 後端開發 > 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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板