function downloadWithName(uri, name) { function eventFire(el, etype){ if (el.fireEvent) { (el.fireEvent('on' + etype)); } else { var evObj = document.createEvent('Events'); evObj.initEvent(etype, true, false); el.dispatchEvent(evObj); } } var link = document.createElement("a"); link.download = name; link.href = uri; eventFire(link, "click"); }
For example, calling
downloadWithName("data:,Hello%2C%20World!", "helloWorld.txt")will prompt the user to download a text file called helloWorld.txt containing "Hello World".