madu
madu

Reputation: 354

Download HTML file using a tag

I'm trying to download a HTML file using an a tag following is my code. It is working in Chrome and Firefox but it does not works in IE

 var a = document.createElement("a");
 a.href = "data:text/html,"+htmlContent;
 a.target      = '_blank';
 a.download = "test.html";
 document.body.appendChild(a);
 a.click();

Any ideas what might causing the issue

Upvotes: 0

Views: 1170

Answers (3)

user458541
user458541

Reputation:

As the download attribute is only a relatively recent addition to HTML5, it's not wise to expect widespread support from browsers just yet.

Currently, the only reliable way to ensure a browser initiates a download when opening a URL is to have the server send the correct headers telling it to do so:

Content-Disposition: attachment; filename=test.html

As the MDN docs will tell you, the attribute is completely unsupported by all versions of IE, meaning it's still a bit too early to rely on the download attribute. I'd recommend server intervention for the time being.

Upvotes: 1

David P
David P

Reputation: 2083

Switch you href to include the actual file name, and then add a

?forcedownload=1

to the end of it. This should work for IE.

Upvotes: 0

Julien Grégoire
Julien Grégoire

Reputation: 17114

download attribute of <a> element is not supported on ie. See http://www.w3schools.com/tags/att_a_download.asp

Upvotes: 4

Related Questions