后台实现表格模板下载功能

public function importCustomer()
    {
    //我没有封装 , 只是这一个地方用 ,所以路径就写死了, 需要的话路径可以设置变量
        $file = '/static/file/supplierCusImport.xlsx';
        $file_lj = str_replace("\\", "/", ROOT_PATH . 'public');
        $files = $file_lj . $file;
        dump($files);
        if (!file_exists($files)) {
            return "文件不存在";
        } else {
            //打开文件
            $file1 = fopen($files, "r");
            //输入文件标签
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: " . filesize($files));
            Header("Content-Disposition: attachment; filename=supplierCusImport.xlsx");
            ob_clean();     // 重点!!!
            flush();        // 重点!!!!可以清除文件中多余的路径名以及解决乱码的问题:
            echo fread($file1, filesize($files));
            fclose($file1);
            exit();
        }
    }

**重点 , 前台不要ajax请求, 这样的话不会有下载, 直接把路由地址写上

window.location.href = 'http://www.xxxxxxx.com/importCustomer'  //

**

发布了5 篇原创文章 · 获赞 0 · 访问量 134

猜你喜欢

转载自blog.csdn.net/Ant_yi/article/details/103803570