简体   繁体   中英

HTML5 and Javascript for Download

The following Javascript will bring up a pdf file in a new browser page:

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? While the HTML5 attribute 'download' doesn't work in IE, I'd like to use it, but I'm not sure how.

The download attribute can be used like this:

<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? 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.

See: Force download through js or query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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