I have a variable in javascript the contents of which are valid for a .csv
file. I want the user to save its contents in a file of his/her choice. I do the following:
HTML
<button type="button" id="downloadCSV">Download CSV</button>
JS
$('#downloadCSV').click(function () {
var csvContent = generateCSV(); //The function generates a valid CSV
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
});
This does two things that I don't want:
- Opens another chrome tab.
- just says 'download' in the windows pop up (Even the extension needs to be added manually). Instead of that I want it to have a default name with extension. Say
foobar.csv
I have gone through similar questions like this but to no avail. How can I achieve this?