访问PHP的时候生成TXT并自动下载。
1.页面增加一个按钮,按钮事件为:
window.location.href="test.php";
2.test.php的代码如下:
第一步:处理中文文件名:
$ua = $_SERVER["HTTP_USER_AGENT"];
$filename = "中文文件名.txt";
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
以上方法可支持下载中文文件名。
第二步:生成TXT文件
header("Content-Type: application/octet-stream");
if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
header('Content-Disposition: attachment; filename*="utf8' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
支持各种浏览器。
第三步:输出内容
直接用echo输出,“\r\n”用以换行。
转载请注明:http://itsshq.com/article-168.html