dtsc14683 2017-04-20 14:27
浏览 259

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

I'm trying different things to download a file via my API which is called by javascript (axios). I can see that my response from the api is Resource id #19 and that it contains the correct information but how do I now download it?

PHP code

$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
  • 写回答

2条回答 默认 最新

  • dqh19413 2017-04-20 14:32
    关注

    php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo. You can not read back from this stream.

    I think you want to use php://input instead to read the response body.

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了