简体   繁体   English

用Javascript下载pdf(在标签中未打开)

[英]Download a pdf (not open in tab) in Javascript

I have a landing page on which there is a form which is filled by user and then it goes to a thank you page (which is another page). 我有一个登陆页面,上面有一个用户填写的表格,然后转到谢谢页面(这是另一个页面)。 On thank you page I simply want to prompt the user for saving a pdf as the thank you page loads. 在“谢谢”页面上,我只想提示用户在加载“谢谢”页面时保存pdf。 Please note I want the file to be saved/downloaded and not opened on the same tab or another tab in the browser. 请注意,我希望文件被保存/下载而不在浏览器的同一选项卡或另一个选项卡上打开。 I have tried multiple solutions but they all seem to open the pdf in the browser itself and not download it. 我尝试了多种解决方案,但是它们似乎都在浏览器中打开了pdf而不下载了。 Some of the methods I have tried are meta tag download, page redirect download, iframe download but they all open the pdf in browser itself and not download it. 我尝试过的一些方法是元标记下载,页面重定向下载,iframe下载,但是它们都在浏览器本身中打开pdf,而不下载。 I just want a simple solution for downloading the file and not opening it in the browser. 我只想要一个简单的解决方案,用于下载文件而不在浏览器中打开它。 Also the pdf should download automatically without clicking any link or button. pdf也应自动下载而无需单击任何链接或按钮。

The document.ready event of the thank you page should trigger a local script that runs a server script to download the PDF. 谢谢页面的document.ready事件应触发运行服务器脚本以下载PDF的本地脚本。 Be sure to set the headers as described above. 确保如上所述设置标题。 If you try to link directly to the PDF, it'll try to load in the browser. 如果您尝试直接链接到PDF,它将尝试在浏览器中加载。

Alternatively, you could create the PDF in a directory that is already set to only download files by setting the .htaccess file for that directory. 或者,您可以通过为该目录设置.htaccess文件,在已经设置为仅下载文件的目录中创建PDF。

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

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