第一个方法是生成txt文件 第二个方式是点击实现下载txt文件
public function createFile(Request $request){
set_time_limit(0);
$date = $request->get('date');
// $path = '/admin/financialAssociation/financialAssociation.json?date='.$date;
// $rest = $this->rqst('get',$path,[]);
$rest = file_get_contents('http://api.php.rongtuojinrong.com/admin/financialAssociation/financialAssociation.json?date='.$date);
$rest = json_decode($rest,true);
$string = '';
foreach($rest['data'] as $k => $v){
$string.= $v['realname'].','.$v['zj_type'].','.$v['ident_no'].','.$v['organizational_code'].','.$v['project_number'].','.$v['ywlx'].','.$v['ywzl'].','.$v['fintran_time'].','.$v['dqsj'].','.$v['amount'].','.$v['ywfsrq'].','.$v['ye'].','.$v['yqje'].','.$v['hkstatus']."\r\n";
}
$filename = './121EXPORTTRADEINFO.txt';
//将数组记录写入到txt文本当中
$insert_st = file_put_contents($filename,$string);
if($insert_st){
return json(['code'=>1,'msg'=>'文件生成成功!']);
}else{
return json(['code'=>0,'msg'=>'文件生成失败!']);
}
}
//实现导出txt文件
public function download(){
$filename = './121EXPORTTRADEINFO.txt';
if(file_exists($filename)){
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filename));
readfile($filename);
unlink($filename);
}else{
//没文件直接跳转到本页面 没用ajax无法给出弹框提示!
$this->redirect('/console/inquiry.hjxh/index');
}
}