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> 
3
  • 1
    Have you looked into html5csv? This question might also help. Commented Aug 23, 2018 at 11:10
  • @AndreiGheorghiu the linked question was my issue exactly thank you! Commented Aug 23, 2018 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. Commented Aug 23, 2018 at 15:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.