0
我試圖快速生成一個CSV文件並允許用戶在API被擊中時下載它。下面是我想象中的使用標題下載CSV在PHP中不起作用
User hit a button ->
jquery do $.get to api ->
api endpoint prepares data ->
api endpoint pass in data to another function to generate csv
這裏,用戶交互是到目前爲止我的代碼
public function test(){
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
// Disable caching
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
header("Pragma: no-cache"); // HTTP 1.0
header("Expires: 0"); // Proxies;
$f = fopen("php://output", "w");
$test_data = array(
array(
'something1' => '2'
),
array(
'somethign2' => '3'
),
);
foreach($test_data as $line){
fputcsv($f, $line, ',');
}
fclose($f);
}
我發現,而不是下載的CSV,瀏覽器只是呈現的結果
2
3
的內容
當我在使用Chrome的網絡中查看它。
我試圖改變內容類型強制下載,但仍然有同樣的結果
任何幫助,將不勝感激。
謝謝
如果將'file.csv'引入引號會發生什麼?像這樣:'header(「Content-Disposition:attachment; filename = \」file.csv \「」);' –