2010-10-06 66 views
0

我有一個函數用於輸出文檔,圖片等:頭()的問題在IE

public function direct($theMimeType, $thePath) 
{ 
    header('Content-type: '.$theMimeType); 
    ob_clean(); // clean output buffer 
    flush(); // flush output buffer 
    readfile($thePath); 
    exit; 
} 

在Firefox的偉大工程。無論是PDF,DOCX還是任何其他文件,該文件都會打開。然而,在IE中它凍結並沒有任何顯示。

這是什麼原因造成的?

編輯:

我加入了一些其他的標題:

public function direct($theMimeType, $thePath) 
{ 
    $aSize = filesize($thePath); 
    $aBegin = 0; 
    $aEnd = $aSize; 
    $aFilename = end(explode('/', $thePath)); 
    $aTime = date('r', filemtime($thePath)); 
    $aContentDisposition = ('application/pdf' === $theMimeType) ? 'inline' : 'atachment'; 
    header('HTTP/1.0 200 OK'); 
    header("Content-Type: $theMimeType"); 
    header('Cache-Control: public, must-revalidate, max-age=0'); 
    header('Pragma: no-cache'); 
    header('Accept-Ranges: bytes'); 
    header('Content-Length:'.($aEnd-$aBegin)); 
    header("Content-Range: bytes $aBegin-$aEnd/$aSize"); 
    header("Content-Disposition: $aContentDisposition; filename=$aFilename"); 
    header("Content-Transfer-Encoding: binary\n"); 
    header("Last-Modified: $aTime"); 
    header('Connection: close'); 
    ob_clean(); // clean output buffer 
    flush(); // flush output buffer 
    readfile($thePath); 
    exit; 
} 

那麼,它在IE現在但仍打開該文件時比Firefox慢得多。在IE瀏覽器打開文件之前似乎有幾秒鐘凍結。

+0

我們需要更多信息,比如「你還發送了什麼其他的頭文件?」 – 2010-10-06 09:56:28

+0

沒有其他的標題。我只是使用Content-type。 – 2010-10-06 10:17:01

+0

@Mark Ba​​ker:檢查我更新的問題。我添加了其他幾個標題。 – 2010-10-06 10:19:36

回答

1

多數那些他aders並不是真的有必要。我更願意保持簡單:

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
header ('Pragma: public'); 
header ('Content-Type: '.$theMimeType); 
header ('Content-Disposition: '.$aContentDisposition.'; filename="'.$aFilename.'"'); 
header ('Content-Transfer-Encoding: binary'); 
header ('Content-Length: '.$aSize); 

請注意Content-Transfer-Encoding標頭結尾處的\ n。

'Pragma:public'是專門用於處理IE和https連接問題的工作周。另一個關鍵區別是引號中的$ aFilename。

+0

是的,沒有引號的文件名'可能是令人困惑的Internet Explorer。 – 2010-10-06 14:46:08

2
  • 直接將此文件下載,不使用任何腳本
  • 確保它在IE
  • 在Firefox中,使用的LiveHTTPHeaders觀看由網絡服務器在Firefox
  • ,使用正在發送的報頭
  • 的LiveHTTPHeaders看你的腳本正在發送的報頭
  • 使你的腳本的頭一樣的網絡服務器的
+0

嗯,我不能直接下載這個文件,因爲它在網絡可訪問的文檔根目錄之外。 – 2010-10-06 10:16:39

+0

@理查德請閱讀整個答案,而不是僅第一行 – 2010-10-06 10:47:11