I have a link with href and download attributes:
<a id="lnk" href="data:text/csv;charset=utf-8,1%2C2%2C3" download="my_file.csv">click to download</a>
When I click on it (in Chrome for example) a csv file "my_file.csv" is downloaded as supposed.
Now I want to be able to cause this action programmatically. So using JQuery I am trying to do:
$('#lnk').click();
or
$('#lnk').trigger("click");
but the file is not downloaded.
Here is the code: http://jsbin.com/yereg/3/edit
I could copy the link address from the link and then just use window.open:
window.open('data:text/csv;charset=utf-8,1%2C2%2C3');
but this way I can't set the file name (link does by download="my_file.csv"
attribute). This solution is fine if there is a way to set the file name.
Remark: in my case Chrome and Firefox should be supported. I don't care about the rest of the browsers.