create file download javascript code example
Example 1: typescript create file and download
var fileContents = "Hello world!";
var filename = "hello.txt";
var filetype = "text/plain";
var a = document.createElement("a");
dataURI = "data:" + filetype +
";base64," + btoa(fileContents);
a.href = dataURI;
a['download'] = filename;
var e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, false,
document.defaultView, 0, 0, 0, 0, 0,
false, false, false, false, 0, null);
a.dispatchEvent(e);
a.removeNode();
Example 2: js download file from webserver
<iframe id="download_iframe" style="display:none;"></iframe>
<script>
document.getElementById('download_iframe').src = "/example/example.txt";
</script>