如何创建不需要右键单击的 PDF 下载链接? [英] How can I create a download link of a PDF that does not require a right click?
问题描述
我在一个网站上工作,访问者应该可以在其中下载 pdf 文件.(有三个链接可供选择,但这无关紧要)我想知道如何制作它以便访问者只需单击链接而不必
I am working on a website where the visitor should be able to download a pdf file. (There are three links to choose from but that is irrelevant) I wanted to know how to make it so that the visitor can simply click the link and not have to
right click > Save (target) As...
我对 PHP 和/或 Javascript 解决方案持开放态度.谢谢.
I am open to PHP and or Javascript solutions. Thanks.
我可以使用 javascript 调用 PHP 并通过 AJAX 保存文件吗?
Can I use javascript to call the PHP and save the file via AJAX?
我最后使用了 Nirmal 的解决方案,因为它是对所有三个文件进行更改最简单的方法.我不需要为三个 PDF 制作 3 个文件,也不需要手动编码开关.BalusC 得到了检查,因为他/她的代码首先启动并且也能做到这一点.
I used Nirmal's solution in the end, since it was the simplest to change for all three files. I didn't need to make 3 files for the three PDF's and I didn't need to hand code the switch. BalusC gets the check though since his/her code was up first and does the trick too.
推荐答案
您基本上需要做的就是设置 Content-Disposition
标题到 attachment
以获得另存为"对话框.这是一个启动 PHP 示例:
All you basically need to do is to set the Content-Disposition
header to attachment
to get a 'Save As' dialogue. Here's a kickoff PHP example:
<?php
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="foo.pdf"');
readfile('/path/to/foo.pdf');
?>
您不能也不想用 Javascript 来做到这一点.
You can't and don't want to do this with Javascript.
重要提示:由于一个糟糕的功能,在 MSIE 中,另存为"对话框中的默认文件名不会从 content-disposition
标头中派生,而是作为最后一部分请求 URL 中的路径信息.要解决此问题,请将 PDF 文件名附加到链接,例如http://example.com/pdf/foo.pdf
.您甚至可以在 PHP 中使用它来读取 pathinfo 中指定的 PDF 文件.下面是 pdf.php
的一个基本示例:
Important note: due to a poor feature, in MSIE the default filename in 'Save As' dialogue won't be derived from the content-disposition
header, it will instead be the last part of the pathinfo in the request URL. To workaround this, append the PDF filename to the link, e.g. http://example.com/pdf/foo.pdf
. You can even make use of it in PHP to read the in the pathinfo specified PDF file. Here's a basic example of pdf.php
:
<?php
$file_name = $_SERVER['PATH_INFO'];
$file = '/path/to/pdf/files' . $file_name;
if (file_exists($file)) {
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
header('HTTP/1.1 404 Not Found');
}
?>
然而,这假设您已经打开了 MultiViews
,以便 /pdf/
将通过这个 PHP 文件,或者至少一个 RewriteRule
从 /pdf/
到 /pdf.php/
.
This however assumes that you've MultiViews
on so that /pdf/
will go through this PHP file, or at least a RewriteRule
from /pdf/
to /pdf.php/
.
这种方法的主要优点是,当您想要添加新的 PDF 文件或更改 PDF 文件名时,您无需更改代码.
The major advantage of this approach is that you don't need to change the code whenever you want to add a new PDF file or change the PDF file name.
您甚至可以通过自动确定和设置正确的内容类型来使其更通用:
You can even make it more generic by automatically determining and setting the correct content type:
<?php
$file_name = $_SERVER['PATH_INFO'];
$file = '/path/to/all/files' . $file_name;
if (file_exists($file)) {
header('Content-Type: ' . mime_content_type($file_name));
header('Content-Disposition: attachment;filename="' . basename($file_name) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
} else {
header('HTTP/1.1 404 Not Found');
}
?>
将它命名为 files.php
左右,然后你就有了一个通用的 PHP 下载器,你可以通过例如 http://example.com/files/foo.pdf来访问它代码>、<代码>http://example.com/files/bar.zip
等.
Name it files.php
or so and then you have a generic PHP downloader which you can access by for example http://example.com/files/foo.pdf
, http://example.com/files/bar.zip
, etcetera.
希望这会有所帮助.
这篇关于如何创建不需要右键单击的 PDF 下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!