0

I'm testing the following piece of Javascript code on the Console before I include it into my application (I found it on SO)

pom = document.createElement('a')
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent("Hello World!"))
pom.setAttribute('download', "IEpomTest")
pom.style.display = 'none'
document.body.appendChild(pom)
pom.click()
document.body.removeChild(pom)

This is working fine on Chrome and the pom.click() downloads a text file with "Hello World!" in it, but when I try it on IE11, the "pom.click()" takes me to this URL "data:text/plain;charset=utf-8,Hello%20World!" and I get the error message "The webpage cannot be displayed".

Anyone has an idea what am I doing wrong?

j08691
  • 204,283
  • 31
  • 260
  • 272
rh4games
  • 962
  • 2
  • 15
  • 38
  • seems like IE does not support data:text/plain urls. – Christoph May 15 '17 at 21:16
  • IE doesn't support the `download` attribute ([Can I use: download](http://caniuse.com/#search=download)). I suggest looking into [FileSaver.js](https://github.com/eligrey/FileSaver.js/) – Patrick Barr May 15 '17 at 21:30

1 Answers1

1

IE will try to open as a website, a better approach might be checking if window.navigator.msSaveBlob is present, and if is, Using this method to download the file

Nahuel Veron
  • 532
  • 6
  • 9