php://output 导出excel 第一次请求OK, 再请求第二次就报错了

php2py

问题描述

导出Excel php://output 直接返回response 第一次请求OK, 再请求第二次就报错了

程序代码


header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename=".$Excel['fileName'].".xlsx");
header('Cache-Control: max-age=0');//禁止缓存
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');

$c = ob_get_contents();
ob_flush();
flush();
$response = response();
$response->withHeaders([
    'Content-Type' => 'application/vnd.ms-excel',
    'Content-Disposition' => 'attachment;filename="'.$fileName.'"',
    'Cache-Control' => 'max-age=0',
])->withBody($c);
ob_clean();
return $response;

报错信息

Cannot modify header information - headers already sent by (output started at E:\\GitProject\\webman-admin\\BackEnd\\app\\common\\controller\\Manage.php:199)

截图报错信息里报错文件相关代码

截图
截图

第二次请求就报错了
截图

操作系统及workerman/webman等框架组件具体版本

window10
webman: 1.4.3
php: 7.4

647 1 1
1个回答

php2py

解决了,原来是我在response之前输出了header内容

  • 暂无评论
年代过于久远,无法发表回答
🔝