2012-04-08 50 views
3

由于我的电子书阅读器(Sony PRS-T1)的内置浏览器非常愚蠢,并且希望将.epub文件作为文本文件而不是下载它们,所以我试图迫使浏览器下载这个.htaccess文件的.epub文件:标题添加内容处理“附件”导致内部服务器错误

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

然而,这会导致内部服务器错误:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

当我离开了Header add Content-Disposition "attachment"没有错误 - 但是,浏览器不会下载该文件:(

我做错了什么?内部服务器错误来自哪里?

[编辑2013-04-11]

我只是赢得了“人气问题,徽章”这个主题,这让我想起了加入一些信息。

我终于成功地迫使索尼PRS-T1的浏览器下载用下面的PHP功能

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(); 
} 

希望帮助别人一天。

+0

你的日志说......? – 2012-04-08 23:37:32

+0

对不起,不是我的服务器 - 也许我可以要求日志,但不知道。 – speendo 2012-04-09 00:15:15

+1

[将Content-Disposition标题设置为仅在某个目录中的文件上附件?](http://support.microsoft.com/kb/3977159/set-content-disposition-header-to-attachment-only-on-文件在一定的导演) – 2015-04-24 02:22:49

回答

3

这可能是答案:

http://diogomelo.net/node/24

The module headers is not enabled by default on Apache. So, we have to enable it manually.

To enable this module, log in as root, and create a symbolic link from mods-available/headers.load to mods-enabled. After that, reload apache and it's done. To do so, I've used this commands.

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

在Windows上的Apache如何实现这一点? – Gregra 2012-08-30 08:19:28

+2

@Gregra打开'httpd。conf'并取消注释这一行:'LoadModule headers_module modules/mod_headers.so'通过删除'#'字符。然后重新启动你的apache服务器。 – Farahmand 2012-09-29 13:17:27

1

您可能还希望确保头模块在你的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 tray图标并选择“Apache - > Apache Modules - > headers_module”或确保它已被选中。

0

的Ubuntu,那里有一条捷径,使Apache2的头模块,使用:

sudo a2enmod headers 

问题解决了^ _^