gpt4 book ai didi

javascript - 通过 JavaScript 中的 API 调用下载文件

转载 作者:行者123 更新时间:2023-12-03 04:30:16 25 4
gpt4 key购买 nike

我正在尝试不同的方法来通过我的 API 下载文件,该 API 由 javascript (axios) 调用。我可以看到 API 的响应是 Resource 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

最佳答案

最后发现无法通过ajax请求下载。只需打开一个带有 url 的新窗口即可正常下载。

示例(客户端)

var win = window.open(`${apiPath}export`, '_blank');
win.focus();

关于javascript - 通过 JavaScript 中的 API 调用下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com