从api下载数据时,如何在Chrome中使下载弹出窗口起作用?

2020-02-22 php codeigniter phpspreadsheet

我已成功将数据从API下载到excel,但是在Chrome中没有下载确认信息。只是将其保存在供应商问题中。我想在chrome下载中显示

public function download()
    {
        $host ="https://jsonplaceholder.typicode.com/posts";
        $process = curl_init($host); 

        curl_setopt($process, CURLOPT_TIMEOUT, 30);


        curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
        $return = curl_exec($process);
        curl_close($process);




        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'Id');
        $sheet->setCellValue('B1', 'Name');
        $sheet->setCellValue('C1', 'Skills');
        $sheet->setCellValue('D1', 'Address');   
         $rows = 2;

         $results=json_decode($return);



        foreach ($results as $val){
            $sheet->setCellValue('A' . $rows, $val->id);
            $sheet->setCellValue('B' . $rows, $val->userId);
            $sheet->setCellValue('C' . $rows, $val->title);
            $sheet->setCellValue('D' . $rows, $val->body);
            $rows++;
        } 


        $writer=new Xlsx($spreadsheet);
        $writer->save('json.xlsx');



    }

Answers

它不是由PHP代码完成的,您需要的是浏览器功能 选中此链接以使Chrome浏览器显示下载内容如何显示Chrome浏览器下载内容

Related