IE 11下载文件的时候发现乱码.
网上查了下资料,了解了一下,主要是user-agent变了,不再是MSIE,面是rv:11.0这样,所以只要服务端判断出是IE 11,处理跟MSIE一样。
GO:
func setDownloadFileName(w http.ResponseWriter, fileName, agent string) {
if strings.Contains(agent, "MSIE") {
fileName = url.QueryEscape(fileName)
fileName = strings.Replace(fileName, "+", "%20", -1)
}
if strings.Contains(agent, "rv:") && strings.Contains(agent, "Gecko") {
fileName = url.QueryEscape(fileName)
fileName = strings.Replace(fileName, "+", "%20", -1)
}
w.Header().Set("Content-Disposition", "attachment;filename=\""+fileName+"\"")
}
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 . '"' ); }
有疑问加站长微信联系(非本文作者)