通过javascript中的API调用下载文件
我正在尝试通过javascript(通过javascript中的API调用下载文件,javascript,php,download,Javascript,Php,Download,我正在尝试通过javascript(axios)调用的API下载文件。我可以从api中看到我的响应是资源id#19,并且它包含正确的信息,但是我现在如何下载它呢 PHP代码 $filename = "Export file"; $delimiter = "\t"; $output = fopen('php://output', 'w'); fwrite($output, "sep=\t" . PHP_EOL); // ... put stuf in document fclose($outp
axios
)调用的API下载文件。我可以从api中看到我的响应是资源id#19
,并且它包含正确的信息,但是我现在如何下载它呢
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
var win = window.open(`${apiPath}export`, '_blank');
win.focus();
php://output 是一个只写流,允许您以与打印和回显相同的方式写入输出缓冲区机制。您无法从该流中读回
我想你想用php://input 而是阅读响应正文。最终发现无法通过ajax请求下载。只要打开一个带有url的新窗口,它就可以正常下载了 示例(客户端)
我不确定我是否理解正确,但php脚本位于服务器端,通过ajax请求进行调用。那么它不一定要在输出中吗?