generate and download a file with javascript? code example
Example 1: javascript download file
$('a#someID').attr({target: '_blank',
href : 'http://localhost/directory/file.pdf'});
Example 2: 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");
// Use of deprecated function to satisfy TypeScript.
e.initMouseEvent("click", true, false,
document.defaultView, 0, 0, 0, 0, 0,
false, false, false, false, 0, null);
a.dispatchEvent(e);
a.removeNode();
Example 3: js download file from webserver
<iframe id="download_iframe" style="display:none;"></iframe>
<script>
document.getElementById('download_iframe').src = "/example/example.txt";
</script>