使用javascript / ajax / jquery强制下载pdf链接 [英] Force download a pdf link using javascript/ajax/jquery
问题描述
假设我们有一个pdf链接 http://manuals.info.apple.com /en/iphone_user_guide.pdf (仅举例来说,让你知道我的服务器上没有该文件,我只有链接)...现在我必须在我的网站上提供一个下载按钮文件。
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.
我尝试了各种各样的东西,比如window.open,href等方法但是它打开了其他窗口上的链接。我知道这是因为现在所有的浏览器都带有一个adobe插件,可以在另一个窗口打开它,但仍然没有任何方式我给用户提供下载选项,而不是打开它,通过客户端脚本..
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 ..
plz help ..
谢谢
plz help.. thanks
推荐答案
使用HTML5下载属性
Use the HTML5 "download" attribute
<a href="iphone_user_guide.pdf" download="iPhone User's Guide.PDF">click me</a>
警告:截至本文撰写时,在IE / Safari中无效,请参阅: caniuse.com/#search=download
Warning: as of this writing, does not work in IE/Safari, see: caniuse.com/#search=download
编辑:如果您正在寻找实际 javascript解决方案,请参阅 lajarre的回答
If you're looking for an actual javascript solution please see lajarre's answer
这篇关于使用javascript / ajax / jquery强制下载pdf链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!