[英]Excel report is opening in browser instaed of asking for download in PHP
我已將服務器從共享服務器移到專用服務器。 在共享服務器中,我的excell報告運行正常,要求下載由PHP創建的下載。 我給了以下標題
header("Content-type: application/vnd.ms-excel; name='excel'") ;
header("Content-Disposition: inline; filename=MBQ_report.xls");
header("Expires: 0") ;
header("cache-control:just-revalidate,post-check:0, pre-check:0") ;
header("Pragma: public")
在我的新服務器中,Excell報告正在瀏覽器中打開,而不是要求下載。 任何人都可以幫助我解決問題。 我可以知道原因嗎?
為了做到這一點, 內聯更改為附件 。
代替header("Content-Disposition: inline; filename=MBQ_report.xls");
放置header("Content-Disposition: attachment; filename=MBQ_report.xls");
奏效了嗎?
有時取決於瀏覽器的設置,它可能被配置為下載xls文件,或者如果xls是瀏覽器的已知文件擴展名,則直接將其打開。 但是您已經配置為打開它,而不提示下載。
改變這個
header("Content-Disposition: inline; filename=MBQ_report.xls");
對此
header("Content-Disposition: attachment; filename=MBQ_report.xls");
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
HTTP上下文中的第一個參數是內聯(默認值,表明可以在網頁內顯示,也可以作為網頁顯示)或附件(指示應下載;大多數瀏覽器會先顯示“另存為”對話框)帶有文件名參數的值(如果存在)
然后,您可以使用.htaccess。
<Files *.xls*>
ForceType application/vnd.ms-excel
Header set Content-Disposition attachment
</Files>
或這個
<FilesMatch "\.(xls|xlsx)$">
ForceType application/vnd.ms-excel
Header set Content-Disposition attachment
</FilesMatch>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.