0

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>
Vereonix
  • 1,341
  • 5
  • 27
  • 54
  • 1
    Have you looked into [html5csv](https://github.com/DrPaulBrewer/html5csv)? [This question](https://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction) might also help. – tao Aug 23 '18 at 11:10
  • @AndreiGheorghiu the linked question was my issue exactly thank you! – Vereonix Aug 23 '18 at 14:44
  • I'll mark it as duplicate than. It will help improve indexing on the other one, so it can be found easier by other people. – tao Aug 23 '18 at 15:59

0 Answers0