强制下载MSI文件

4

我今天可能有点迟钝,看不懂这个。

我的网站上有一个页面应该强制下载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" />

但是它拒绝下载文件。 非常感谢任何指导。
3个回答

1
假设使用Apache,如果您想要使用.htaccess文件,可以添加以下内容:
AddType application/octet-stream .msi

根据此来源,您可以直接链接到文件。

0

<meta>标签适用于它们嵌入的页面。它们不会改变文件下载的方式,这被认为是完全独立的页面。

您应该像这样拥有<a href="downloadmsi.php" target="_new" />download</a>,以便下载发生在不同的窗口而不是说明页面中。


0

我找到了这个链接,帮助我创建了一个PHP文件来链接到指定的.msi下载。第一次就成功了。

可见的下载页面,downloads.php,也许可以用。

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

从上面的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);

另外,我不确定是否必要,但我已经按照Marc B的建议在.htaccess文件中添加了AddType application/octet-stream .msi


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接