I need to allow users to download a CSV file, I have the CSV as a string value. The following function works in Chrome, but not for IE 10/11, Edge or Firefox.
function downloadCSV(CSV, fileName) {
var data = "data:text/json;charset=utf-8," + encodeURIComponent(CSV);
var downloader = document.createElement('a');
downloader.setAttribute('href', data);
downloader.setAttribute('download', fileName+'.csv');
downloader.click();
};
This works flawlessly in Chrome.
Other browsers are able to see the CSV variable in a console.log, but won't download. Edge will output a warning in console with a giant link containing the CSV contents which if I click with show the CSV as a string variable in the Debugger window. Firefox gives no error or warning when the above function is triggered.
I've tried adding compatibility for different browsers, but it doesn't change anything.
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<!--<add name="X-UA-Compatible" value="IE=10" />
<add name="X-UA-Compatible" value="IE=11" />-->
<add name="X-UA-Compatible" value="IE=edge" />
</customHeaders>
</httpProtocol>
</system.webServer>