如何使用js自动下载PDF? [英] How to download PDF automatically using js?

查看:803
本文介绍了如何使用js自动下载PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的方案是自动下载PDF文件,然后用户填写它,当点击PDF中的提交按钮时,它连接到java servlet并将其保存在数据库中。

  1  - 用户点击按钮
2 - 自动完成JavaScript代码运行和PDF文件下载
3 - 使用JavaScript自动打开文件
4 - 用户填充&am​​p;按提交
5 - 提交servlet代码运行并保存数据到db

在我的应用程序只是第二点缺失。请提供代码如何使用JavaScript与扩展程序交互以自动下载文件。
我只是想下载这个文件。 解决方案

使用下载属性



< pre class =lang-js prettyprint-override> var link = document.createElement('a');
link.href = url;
link.download ='file.pdf';
link.dispatchEvent(new MouseEvent('click'));


My scenario is that PDF file download automatically, then user fills it and when click on submit button in PDF it connect to java servlet and save it in DB.

1 - User click on Button
2 - JavaScript code run and PDF file download automatically
3 - open file using JavaScript automatically
4 - user fills & press submit
5 - after submit servlet code run and save data in db

In my Application just the 2nd point is missing. Please provide code how to interact with extension using JavaScript to download file automatically. I just want to download the file.

解决方案

Use the download attribute.

var link = document.createElement('a');
link.href = url;
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));

这篇关于如何使用js自动下载PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆