简体   繁体   English

纯JavaScript或jQuery文件下载功能

[英]Pure JavaScript or jQuery file download function

Using Javascript, I'm trying to establish an appropriate method to display a PDF file in a new browser tab. 我正在尝试使用Javascript建立适当的方法,以在新的浏览器标签中显示PDF文件。 If the browser doesn't have an in-built PDF viewer, I'd like the user to have the option to download the file. 如果浏览器没有内置的PDF查看器,我希望用户可以选择下载文件。 My research has led me to John Culviner's jQuery plugin jquery.fileDownload.js here , and that looks a great fit. 我的研究使我对约翰Culviner的jQuery插件jquery.fileDownload.js 这里 ,这看起来非常适合。 The issue I have however is that I don't have access to the web server hosting the site in order to upload the plugin. 但是,我的问题是我无法访问托管该站点的Web服务器来上传插件。 I've also been unable to find a CDN that hosts the plugin and which I could link to. 我也一直找不到托管该插件的CDN,并且可以链接到该CDN。

Does anyone have a few 'best practice' suggestions to achieve my goals that I can research further? 有没有人提出一些“最佳实践”建议来实现我可以进一步研究的目标? Thank you. 谢谢。

There is no need to involve JS. 无需涉及JS。 Just link to the PDF. 只需链接到PDF。 Use the target attribute to trigger a new tab. 使用target属性触发一个新选项卡。

<a href="foo.pdf" target="_blank">

If the server lies about the content-type of the PDF or sends a header that indicates it should be downloaded instead of rendered, then you are out of luck. 如果服务器是关于PDF的内容类型的,或者发送的标题指示应该下载而不是渲染它,那么您很不走运。

If you really want to use JS, then: 如果您真的想使用JS,则:

window.open('foo.pdf');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM