【发布时间】:2012-04-08 23:03:43
【问题描述】:
由于我的电子书阅读器(Sony PRS-T1)的内置浏览器非常愚蠢,并且想将 .epub 文件作为文本文件打开而不是下载它们,我试图强制浏览器下载 .epub 文件这个 .htaccess 文件:
<FilesMatch "\.(?i:epub)$">
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
但是,这会导致内部服务器错误:
内部服务器错误
服务器遇到内部错误或配置错误,并且 无法完成您的请求。
请联系服务器管理员 webmaster@localhost 和 通知他们错误发生的时间,以及您可能会做的任何事情 这样做可能会导致错误。
有关此错误的更多信息可能在服务器错误中可用 记录。
当我省略 Header add Content-Disposition "attachment" 时,没有错误 - 但是,浏览器不会下载文件:(
我做错了吗?内部服务器错误来自哪里?
[编辑 2013-04-11]
我刚刚为这个帖子赢得了“热门问题徽章”,这让我想起了添加一些信息。
我终于设法使用以下 php 函数在 Sony 的 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();
}
希望有一天能对某人有所帮助。
【问题讨论】:
-
抱歉,不是我的服务器 - 也许我可以索要日志,但我不知道。
-
我有一台 Sony PRS-T3,它对我有帮助,谢谢!在我的测试中,您只需要正确的 MIME 类型即可成功下载。