6

重複の可能性:
pdf のダウンロードを自動的に強制する方法は?

他のフォーラムもチェックしましたが、何を言っているのかわかりません。私は人々が解決策を得ると思われるこのリンクをチェックしました:

http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

PDF をダウンロードするためのボタンを作成したいのですが、ファイルが大きすぎてオンラインで表示できません。そのため、そのボタンでブラウザに強制的にファイルをダウンロードさせたいのです。ユーザーに尋ねるかどうか。さまざまな PDF 名のファイルがあるため、このボタンをさまざまな PDF ファイルの PDF 形式でダウンロードできるようにする必要があります。

私はさまざまな Web をチェックしましたが、htaccess を使用して実行できるようですが、正直なところ、これを行う方法についての手がかりがありません。誰かがこれを行う方法を教えてくれませんか!?!?! CSS/html 経由の簡単な方法はありますか??

私は、このような小さな詳細のために長い間立ち往生してきました。

私のコードは単純です:

<div class="Container for icons">
<a href="hugefile.pdf" alt=""><img href="icon.png" alt=""/>
</div>

誰か私にこれを渡してくれませんか?

4

2 に答える 2

14

これを行うには、クライアントに返される HTTP ヘッダーを変更する必要があります。

PHP コードの例を次に示します。

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // For download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);  //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb
于 2012-08-16T15:49:40.537 に答える
5

あなたの代わりにAddType試すことができますForceType

<FilesMatch "\.(pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>
于 2012-08-16T15:53:14.623 に答える