From the following code I'm creating a dynamic anchor tag which downloads a file. This code works well in Chrome but not in IE. How can I get this working
<div id="divContainer"> <h3>Sample title</h3> </div> <button onclick="clicker()">Click me</button> <script type="text/javascript"> function clicker() { var anchorTag = document.createElement('a'); anchorTag.href = "http://cdn1.dailymirror.lk/media/images/finance.jpg"; anchorTag.download = "download"; anchorTag.click(); var element = document.getElementById('divContainer'); element.appendChild(anchorTag); } </script>
The download attribute only works for same-originl URLs. So if the href is not the same origin as the site, it won't work. In other words, you can only download files that belongs to that website. This attribute follows the same rules outline in the same-origin policy.
Approach 1: Using Download attribute The download attribute simply uses an anchor tag to prepare the location of the file that needs to be downloaded. The name of the file can be set using the attribute value name, if not provided then the original filename will be used.
The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink. The optional value of the download attribute will be the new name of the file after it is downloaded.
HTML: Use the anchor element download attribute to prompt the user to download a resource. The download attribute on an anchor tag pops up a Save dialog to download the resource, instead of navigating to it. e.g. Using an attribute value, such as download="cat-1.
Internet Explorer does not presently support the Download
attribute on A
tags.
See http://caniuse.com/download and http://status.modern.ie/adownloadattribute; the latter indicates that the feature is "Under consideration" for IE12.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With