下载文件并不暴露文件路径

1、HTML页面

<button type="button" class="mbDownload">下载模板</button>
<script type="text/javascript">
	//下载模板
	$('.mbDownload').click(function(){
		$.post(
			"{:U('Index/mbDownload')}",
			function(res){
				var form = document.createElement('form');
				form.setAttribute('style','display:none');
				form.setAttribute('target','');
				form.setAttribute('method','post');
				form.setAttribute('action',res);
				
				var input = document.createElement('input');
				input.setAttribute('type','hidden');
				input.setAttribute('name','exportPostTime');
				input.setAttribute('value','');
				document.body.appendChild(form);
				form.appendChild(input);
				form.submit();
			}
		)
	})
</script>

2、PHP文件

/* 
 * 模板下载
 */
public function mbDownload(){
	$url = "http";
	if($_SERVER['HTTPS']=='on') $url .= "s";
	$url .= "://".$_SERVER['SERVER_NAME'];
	
	echo $url.'/Public/模板表格.xlsx';
	exit();
}

你可能感兴趣的:(下载文件并不暴露文件路径)