【问题标题】:force download of msi file强制下载 msi 文件
【发布时间】:2012-04-04 16:18:59
【问题描述】:

我一定是度过了忙碌的一天。我似乎无法弄清楚这一点。

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

但这拒绝下载文件。 任何指针都非常感谢。

【问题讨论】:

    标签: php html http-headers download


    【解决方案1】:

    假设 Apache,如果您想使用 .htaccess 文件,您可以添加以下内容:

    AddType application/octet-stream .msi
    

    根据this source。然后,您可以链接到该文件。

    【讨论】:

      【解决方案2】:

      &lt;meta&gt; 标签适用于它们嵌入的页面。它们不会改变文件的下载方式,文件被认为是一个完全独立的页面。

      您应该有类似&lt;a href="downloadmsi.php" target="_new" /&gt;download&lt;/a&gt; 这样的信息,以便下载发生在与说明页面不同的窗口中。

      【讨论】:

        【解决方案3】:

        我发现 this link 帮助我创建了一个 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-12-31
          • 1970-01-01
          • 2013-06-20
          • 2013-01-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多