javascript HTML 强制用户从 URL 下载 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26761653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:27:39  来源:igfitidea点击:

HTML force user to download PDF file from URL

javascripthtmlurl

提问by user1539793

I am getting server side URL to my HTML,on click of that URL PDF file is opening in browser itself,Since the PDF is larger Size I want to force the user to download it(saveas pop up) Please help me

我正在获取我的 HTML 的服务器端 URL,点击该 URL 时,PDF 文件会在浏览器中打开,由于 PDF 较大,我想强制用户下载它(弹出保存)请帮助我

回答by Kristian

You don't need javascript to do this. In a modern browser you can simply do <a href="somepathto.pdf" download="filename">

你不需要 javascript 来做到这一点。在现代浏览器中,你可以简单地做<a href="somepathto.pdf" download="filename">

Related post:

相关帖子:

回答by FLX

You can use the HTML5 attribute download :

您可以使用 HTML5 属性下载:

<a href="link" download="filename"/>

You can also make a PHP page that will read the PDF and force the download, by using these functions :

您还可以使用以下函数制作一个 PHP 页面来读取 PDF 并强制下载:

header('Content-Type: application/pdf');
header('Content-disposition: attachment; filename=filename.pdf');
exit(readfile(yourPDF));

(PHP is useful if you want to prevent the users to delete the download attribute, but it may be too much)

(如果你想阻止用户删除下载属性,PHP 很有用,但可能太多了)

回答by David Hellsing

回答by Abomusab Revo

try this

试试这个

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