简体   繁体   English

用于下载的HTML5和Javascript

[英]HTML5 and Javascript for Download

The following Javascript will bring up a pdf file in a new browser page: 以下Javascript将在新的浏览器页面中显示pdf文件:

var url = "http://company.com/myfile.pdf";
window.location.href = url;

How can I change this so that the script initiates a download of the file, given the url? 如何更改此设置,以便脚本在给定URL的情况下启动文件下载? While the HTML5 attribute 'download' doesn't work in IE, I'd like to use it, but I'm not sure how. 虽然HTML5属性“下载”在IE中不起作用,但我想使用它,但我不确定如何使用它。

The download attribute can be used like this: download属性可以像这样使用:

<a href="./directory/yourfile.pdf" download>Download the pdf</a>

But I don't have a link like that on my page. 但我的页面上没有这样的链接。 Can I create a dummy tag and assign an href? 我可以创建一个虚拟标签并分配一个href吗? But I'm not sure how to initiate the download programmatically. 但我不确定如何以编程方式启动下载。

I don't think you're allowed to trigger a download using javascript due to security reasons. 由于安全原因,我认为您不允许使用javascript触发下载。

See: Force download through js or query 请参阅: 通过js或查询强制下载

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

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