【发布时间】:2017-01-23 02:11:07
【问题描述】:
这个问题有多种变体。不幸的是,他们无法帮助我。
以下代码运行良好(使用 slim)。如果不使用 slim,则为用户 header()。
$response = $res->withHeader('Content-Description', 'My File transfer')
->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->withHeader('Content-Disposition', 'attachment;filename="'.basename($filePath).'"')
->withHeader('Expires', '0')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', filesize($filePath));
readfile($filePath);
我尝试了其他答案中的 ob_start、flush、end 组合,但没有任何帮助。
我能够在服务器上打开文件,但是当我通过上述代码下载后尝试打开它时 Excel 无法打开它 - Excel 无法打开文件,因为文件格式或文件扩展名无效。
【问题讨论】: