我有一个 API,它返回 csv 内容作为响应,我想以 Excel 格式下载,我尝试了什么:
HTML:
<form id="test_form" accept-charset="utf-8" method="POST" action="http://prime.dev/index.php//data/reporting/downloadCSVFile.json"></form>
<a id="downloadcsv" class="min-button-silver">Download to Excel</a>
JS:
jQuery("#downloadcsv").click(function(e){ jQuery('#test_form').submit();
});
PHP
public function downloadCSVFile()
{
$data = $this->reporting_service->fetchCSV($request); //API request
header("Content-Type: application/csv");
header("Content-Disposition: attachment; filename=test.csv");
//header("Content-Length: ".$thesize);
header("Pragma: no-cache");
header("Expires: 0");
//Will get the response from API like the below;
$data = 'clicks|conversions|ctr|cvr|impressions|invalid_clicks|currency.id
4220894|127984|.1962348497109191647|.0059484688672145487|2150940063|557193|163';
echo $data;
}
文件下载为 test.csv,但在记事本中打开,你们中的任何人都可以告诉我,这里出了什么问题。
提前致谢和问候