IE 11에서 파일을 다운로드할 때 잘못된 문자를 발견했습니다.
온라인으로 정보를 확인한 결과 주된 이유는 사용자 에이전트가 더 이상 MSIE가 아니라 rv로 변경되었기 때문이라는 것을 알았습니다. 11.0(서버가 IE 11이라고 판단하고 MSIE와 동일하게 처리하는 한).
이동:
<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에서 다운로드한 중국어 왜곡 파일을 해결하는 방법을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.