标头添加内容处置"attachment"导致内部服务器错误



由于我的电子书阅读器(Sony PRS-T1)的内置浏览器非常愚蠢,并且想将.epub文件作为文本文件打开而不是下载它们,我试图强制浏览器下载带有此.htaccess文件的.epub文件:

<FilesMatch ".(?i:epub)$">
  ForceType application/octet-stream
  Header add Content-Disposition "attachment"
</FilesMatch>

但是,这会导致内部服务器错误:

内部服务器错误

服务器遇到内部错误或配置错误,并且 无法完成您的请求。

请联系服务器管理员,webmaster@localhost和 告知他们错误发生的时间,以及您可能的任何内容 已经这样做了,这可能导致错误。

有关此错误的详细信息,请参阅服务器错误 .log。

当我省略Header add Content-Disposition "attachment"时没有错误 - 但是,浏览器不会下载文件:(

我做错了什么吗?内部服务器错误从何而来?

[编辑 2013-04-11]

我刚刚为这个线程赢得了"流行问题徽章",这让我想起了添加一些信息。

我终于设法使用以下php函数在索尼的PRS-T1浏览器上强制下载

function startDownload($path, $mimeType) {
if(!file_exists($path)) {
// File doesn't exist, output error
exit('file not found');
} else {
$size = filesize($path);
$file = basename($path);
// Set headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename="$file"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
}
exit();
}

希望有一天能帮助某人。

这可能是答案:

http://diogomelo.net/node/24

默认情况下,Apache 上不启用模块标头。所以,我们必须 手动启用它。

要启用此模块,请以 root 身份登录,然后从 mods-available/headers.load to mods-enabled.之后,重新加载 apache 它完成了。为此,我使用了此命令。

su - cd
/etc/apache2/mods-enabled/ ln -s ../mods-available/headers.load
headers.load sh /etc/init.d/apache2 force-reload

您可能还需要确保在 htaccess 文件中使用它之前启用标头模块。如果未启用标头模块,则以下行将生成错误:

Header set Content-Disposition "attachment"

下面是一个示例,仅当启用了标头模块时才强制下载 MP3 文件:

<IfModule mod_headers.c>
    <FilesMatch ".(mp3|MP3)$">
        ForceType audio/mpeg
        Header set Content-Disposition "attachment"
        Allow from all
    </FilesMatch>
</IfModule>

注意:它不会启用模块,如果未启用模块,它只会忽略 IfModule 标签中的任何内容。

要启用 apache 模块,您需要编辑 httpd.conf 文件,或者在 wamp 服务器中,您可以单击 wamp 托盘图标并选择"Apache -> Apache 模块 -> headers_module"或确保选中它。

对于 Ubuntu,有一个启用 Apache2 标头模块的快捷方式,使用:

sudo a2enmod headers

问题已解决 ^_^

最新更新