【发布时间】:2012-08-22 01:36:32
【问题描述】:
我正在使用PHPExcel 在我的 PHP 应用程序中动态生成 Excel 文件。我使用的是Excel2007 格式。
当用户访问创建并强制下载 Excel 文件的 URL 时,所有浏览器除了移动版 Safari(iPhone 和 iPad)都运行良好。
这是我的标题和readfile 方法:
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-disposition: attachment; filename=' . $file_name . '.xlsx;');
header('Content-Length: ' . filesize($path_to_file . '.xlsx'));
readfile($path_to_file . '.xlsx');
当我在移动 Safari 中浏览到应该下载 .xlsx 文件的 URL 时,我实际上可以看到代表文件每个工作表的选项卡,但我看不到像这样的实际数据:
此外,我还遇到了另外两种奇怪的行为:
- 如果我在桌面浏览器上下载此文件并将其通过电子邮件发送给自己,然后使用 iOS 中的邮件应用程序打开它,该文件将正确显示。
- 如果我随后从 iOS 中的邮件应用程序中获取该附件并导入到 Dropbox,它不会正确显示(它的显示与上面的屏幕截图相同)。
- 在 Chrome 中,文件正确下载并按预期在 Excel 甚至 Numbers 中打开,但在控制台中我看到此消息:
Resource interpreted as Document but transferred with MIME type application/vnd.ms-excel:
另外,根据 PHPExcel 文档,代替 readfile 我也尝试过:
$objWriter->save('php://output');
然而,这会在移动 Safari 中产生错误,内容如下:
OfficeImportErrorDomain Error 912
为了消除Content-type 的问题,我尝试将Content-type 调整为其他值(例如application/vnd.ms-excel 甚至application/download)。不幸的是(尽管并不奇怪)这些也不起作用。
非常感谢任何指导。
【问题讨论】:
-
stackoverflow.com/questions/3479432/… 中的标头解决方案是否有效?这似乎是一个非常相似的问题
标签: php mobile-safari phpexcel xlsx force-download