当用户点击“下载PDF”链接时,我希望出现下载提示框,并让用户能够下载文件。
目前,用户只是被转移到PDF文件的地址。
例如:
目前,用户只是被转移到PDF文件的地址。
例如:
<a..[what goes here??]..>Download PDF</a>
看起来需要使用JavaScript和PHP的组合来完成这个任务。
有人可以给出一个例子吗?
<a..[what goes here??]..>Download PDF</a>
看起来需要使用JavaScript和PHP的组合来完成这个任务。
有人可以给出一个例子吗?
使用以下代码将页面重定向到PHP页面:
<?php
header('Content-disposition: attachment; filename=movie.mpg');
header('Content-type: video/mpeg');
readfile('movie.mpg');
?>
<a href
代码需要指向特定页面,而readfile会调用你的资源。
更多阅读
顺便说一句,我同意你不应该覆盖浏览器的设置,但有时当老板要求时,你只能这样做。<Files *.pdf>
Header set Content-Disposition attachment
</Files>
You can also use the following code:
<?php
$file = filter_input(INPUT_GET, 'file');
$filename = basename($file);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"{$filename}\";");
readfile("your file path {$file}");
?>
Content-Disposition: attachment; filename=<default filename to save as>
另一方面,我并不认为覆盖用户的浏览器配置有什么意义,因为通常情况下,浏览器会告诉我们PDF文档应该是在浏览器中打开、在独立的PDF查看器中打开或保存到磁盘。
download
属性。<a href="http://love4cats.com/kitten-catalogue.pdf" download>Download the CATalogue</a>
你的意思不是很清楚。
目前用户只是被转移到PDF文件的地址。
但我建议一个简单的方法:
<a href='http://example.com/download.pdf'>Download PDF</a>
下载或打开文件的选择更多是浏览器的设置。
我不相信有一个神奇的JavaScript/PHP解决方案来解决您的问题,原始的HTML:
<a href="docco.pdf" title="this is a link to a pdf">download the PDF</a>
代码会执行所需的操作,它将指示浏览器请求资源“docco.pdf”,由浏览器(和任何插件)决定如何处理。
例如,有一个 Firefox 插件 "PDF 下载",它提供了一个弹出窗口,让用户选择如何处理。
download
属性。(不确定哪些浏览器支持,但您可以添加它。) - Darkwater