2012-04-08 51 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 

問題解決了^ _^