how to download something with js 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");
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>
Example 4: how to set a var in js to be a download
var textToSave = 'this is a test';
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
hiddenElement.target = '_blank';
hiddenElement.download = 'myFile.txt';
hiddenElement.click();