生成txt文件并实现下载功能

第一个方法是生成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');
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值