目标:使用php,通过ajax请求的方式生成一个excel,然后下载。
思路:大致思路是发送一个ajax请求到后台,后台php处理后生成一个excel文件,然后把生成的文件放到一个临时目录,然后把文件链接返回给前端。前端接受到后,然后通过给定的地址,去下载该文件....
代码实现:
前端部分:
<pre name="code" class="html">function outExcel(){
var allSelect = $('._newId:checked').length;
if(allSelect == 0){
layer.msg('请选择需要导出的记录');
return false;
}
var ids = [];
$('._newId:checked').each(function(){
ids.push($(this).val());
});
var params = {id:ids};
$.post("{:U('Home/Saidi/outExcel')}",params,function(res){
if(res.status){
window.location.href = ("{:U('Home/Saidi/download')}?file="+res.url+'&token='+res.token);
}e