首先分享一下CI中檔案強制下載時的header設定。
if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE){header('Content-Type: "'.$mime.'"');header('Content-Disposition: attachment; filename="'.$filename.'"');header('Expires: 0');header('Cache-Control: must-revalidate, post-check=0, pre-check=0');header("Content-Transfer-Encoding: binary");header('Pragma: public');header("Content-Length: ".strlen($data));}else{header('Content-Type: "'.$mime.'"');header('Content-Disposition: attachment; filename="'.$filename.'"');header("Content-Transfer-Encoding: binary");header('Expires: 0');header('Pragma: no-cache');header("Content-Length: ".strlen($data));}其他的都比較好理解,主要是關於Cache-control和Pragma的設定讓人比較迷惑。
關於Cache-Control的must-revalidate:強制頁面不緩衝,作用與no-cache相同,但更嚴格,強制意味更明顯。詳細作用請參考:http://hi.baidu.com/chenleibupt/blog/item/9627bec6932e5a179c163df2.html
關於post-check和pre-check:Internet Explorer 5對於HTTP頭資訊使用兩種新的時間間隔指示:pre-check 和post-check。pre-check副檔名定義了這樣一段時間間隔(以秒記):即在這段時間間隔之後,一個對象在顯示給使用者之前應被選中進行更新。選中對象也可以發生在該對象已經顯示給使用者之後,但是,要保證在使用者下次想要看這個對象時,被快取起來的副本是更新過的。post-check副檔名定義了這樣一段時間間隔(以秒記):即在這段時間之後,在顯示給使用者之前,該對象被選中進行更新。即post-check=0,pre-check=0是IE5.0才有的防cache聲明。(參考自http://bbs.chinaunix.net/thread-704320-1-1.html和http://blog.sina.com.cn/s/blog_5595d51401000b23.html)
關於Pragma:no-cache,跟Cache-Control: no-cache相同。Pragma: no-cache相容http 1.0 ,Cache-Control: no-cache是http 1.1提供的。因此,Pragma: no-cache可以應用到http 1.0 和http 1.1,而Cache-Control: no-cache只能應用於http 1.1.
關於Pragma:public 作用未知,還請閱讀本篇文章的各位大俠給予解釋。