使用 javascript/ajax/jquery 强制下载 pdf 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3077242/
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
Force download a pdf link using javascript/ajax/jquery
提问by Dhananjay
suppose we have a pdf link "http://manuals.info.apple.com/en/iphone_user_guide.pdf"(just for example and to let u know that file is not on my server, i only have the link)...now i have to provide a button on my site that will download the file.
假设我们有一个 pdf 链接“ http://manuals.info.apple.com/en/iphone_user_guide.pdf”(只是为了让你知道该文件不在我的服务器上,我只有链接)。 .现在我必须在我的网站上提供一个按钮来下载文件。
i have tried various things like window.open, href etc. methods but it open the link on other window. i know thats because now all browser comes with a adobe plugin which opens it in another window, but still isnt there any way i give the user the option of download rather than opening it, through client side scripting ..
我尝试了各种方法,例如 window.open、href 等方法,但它会在其他窗口上打开链接。我知道那是因为现在所有浏览器都带有一个 adobe 插件,它可以在另一个窗口中打开它,但仍然没有任何方式让用户可以选择下载而不是打开它,通过客户端脚本..
plz help.. thanks
请帮忙..谢谢
回答by lajarre
Here is a Javascriptsolution (for folks like me who were looking for an answer to the title):
这是一个Javascript解决方案(适用于像我这样正在寻找标题答案的人):
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var evt = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': false
});
save.dispatchEvent(evt);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE < 11
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
source: http://muaz-khan.blogspot.fr/2012/10/save-files-on-disk-using-javascript-or.html
来源:http: //muaz-khan.blogspot.fr/2012/10/save-files-on-disk-using-javascript-or.html
Unfortunately the working for me with IE11, which is not accepting new MouseEvent. I use the following in that case:
不幸的是,我在 IE11 上工作,它不接受新的 MouseEvent。在这种情况下,我使用以下内容:
//...
try {
var evt = new MouseEvent(...);
} catch (e) {
window.open(fileURL, fileName);
}
//...
回答by sarink
Use the HTML5 "download" attribute
使用 HTML5“下载”属性
<a href="iphone_user_guide.pdf" download="iPhone User's Guide.PDF">click me</a>
Warning: as of this writing, does not work in IE/Safari, see: caniuse.com/#search=download
警告:在撰写本文时,在 IE/Safari 中不起作用,请参阅:caniuse.com/#search=download
Edit: If you're looking for an actualjavascript solution please see lajarre's answer
编辑:如果您正在寻找实际的javascript 解决方案,请参阅lajarre 的答案
回答by Benny
With JavaScript it is very difficult if not impossible(?). I would suggest using some sort of code-behind language such as PHP, C#, or Java. If you were to use PHP, you could, in the page your button posts to, do something like this:
使用 JavaScript 是非常困难的,如果不是不可能的话(?)。我建议使用某种代码隐藏语言,例如 PHP、C# 或 Java。如果您要使用 PHP,您可以在按钮发布到的页面中执行以下操作:
<?php
header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=filename.pdf');
readfile("http://manuals.info.apple.com/en/iphone_user_guide.pdf");
?>
This also seems to work for JS (from http://www.phpbuilder.com/board/showthread.php?t=10149735):
这似乎也适用于 JS(来自http://www.phpbuilder.com/board/showthread.php?t=10149735):
<body>
<script>
function downloadme(x){
myTempWindow = window.open(x,'','left=10000,screenX=10000');
myTempWindow.document.execCommand('SaveAs','null','download.pdf');
myTempWindow.close();
}
</script>
<a href=javascript:downloadme('http://manuals.info.apple.com/en/iphone_user_guide.pdf');>Download this pdf</a>
</body>
回答by sohail ansari
回答by jerrygarciuh
If htaccess is an option this will make all PDF links download instead of opening in browser
如果 htaccess 是一个选项,这将使所有 PDF 链接下载而不是在浏览器中打开
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
回答by intrepidis
In javascript use the preventDefault() method of the event args parameter.
在 javascript 中使用 event args 参数的 preventDefault() 方法。
<a href="no-script.html">Download now!</a>
$('a').click(function(e) {
e.preventDefault(); // stop the browser from following
window.location.href = 'downloads/file.pdf';
});

