2012-04-04 129 views
3

我必須有一個密集的一天。我似乎無法弄清楚這一點。強制下載msi文件

我在我的網站上有一個頁面,它應該強制下載msi文件,但是我想離開頁面的html並顯示下載說明。

到目前爲止,我曾嘗試在HTML下面的標記(注意,我只能訪問此頁面的主體)

<meta http-equiv="Refresh" content="0; URL=file.msi"> 

但在Firefox這表明二進制文件爲亂碼。

接下來我嘗試下面的PHP插入

$file = "file.msi"; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 5'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 

但是這個關閉的標籤/窗口,並不會離開安裝說明。

最後,我嘗試以下回到HTML:

<META HTTP-EQUIV="Content-Type" CONTENT="application/octet-stream"> 
<meta http-equiv="content-disposition" content="attachment; filename=file.msi" /> 

但這拒絕下載文件。 非常感謝任何指針。

+0

請參閱下面的答案。 – Whymarrh 2012-04-04 22:42:49

回答

0

<meta>標籤適用於它們嵌入的頁面。它們不會更改文件的下載方式,這被認爲是一個完全獨立的頁面。

您應該有類似<a href="downloadmsi.php" target="_new" />download</a>的東西,以便下載發生在與指令頁面不同的窗口中。

0

假設Apache的,如果你想使用.htaccess文件,你可以添加以下內容:

AddType application/octet-stream .msi 

this source。然後您可以鏈接到該文件。

0

我發現this link,幫助我創建一個PHP文件鏈接到指定的.msi下載。第一次工作。

可見下載頁面,downloads.php,也許

<a href="/downloads/?version=App.Installer.msi">download now</a> 

Sever的處理頁面的HREF以上,即example.com/downloads/index.php

$root = realpath($_SERVER["DOCUMENT_ROOT"]); 
$file_dl = $_GET['version']; 
header('Content-disposition: attachment; filename='.$file_dl); 
header('Content-type: application/x-ole-storage'); 
readfile($root.'/downloads/'.$file_dl); 

而且,不知道這是否是必要的,但我添加到.htaccess文件:AddType application/octet-stream .msi建議Marc B