我正在尝试通过我的API下载文件的不同事情,该API由javascript调用(axios
)。我可以看到我从api的回复是Resource id #19
,它包含正确的信息,但我现在如何下载它?通过javascript调用通过javascript下载文件
PHP代码
$filename = "Export file";
$delimiter = "\t";
$output = fopen('php://output', 'w');
fwrite($output, "sep=\t" . PHP_EOL);
// ... put stuf in document
fclose($output);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=' . $filename . '.csv');
header('Content-Type: text/csv; charset=utf-8');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $output; // also tried readfile($output) but it triggers an error that it needs a valid path
我不知道我理解正确的,但是,PHP脚本是坐在服务器端,并正在通过Ajax请求调用。那么它不需要在输出中吗? – NealVDV