通过ajax在php中下载csv

时间:2019-03-30 09:58:34

标签: php jquery ajax export-to-csv

当我生成csv时,它返回resourceid#7,但我无法下载我的csv。以下是我在ajax调用中调用的功能。

  public function ExportShipments(){
        $jsondata = $this->input->post();
            // disable caching
        $now = gmdate("D, d M Y H:i:s");
        header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
        header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
        header("Last-Modified: {$now} GMT");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: text/csv; charset=utf-8");
        header("Content-Disposition: attachment;filename=\"ShipmentManifest.csv\"");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
         $file = fopen('php://output', 'w');
         $header = array("AWBNO","Status");

         // $exportData = $this->Error_model->getExportData($date,$clientid);
        // echo gettype($jsondata); die;
         $csvData = array();
         $data = json_decode($jsondata['result'], true);

         foreach($data as $row){
             $row1['AWBNO'] = $row['ShippingID'];
             $row1['Status'] = $row['response'];
             $csvData[] = $row1;
         }
         fputcsv($file, $header);
         foreach ($csvData as $key=>$line){
           fputcsv($file,$line);
         }
         fclose($file);
         die();
      }

当我打印$ file时,它返回资源ID 我希望以csv格式下载文件

0 个答案:

没有答案