How to download CSV using a href with a # (number sign) in Chrome? How to download CSV using a href with a # (number sign) in Chrome? google-chrome google-chrome

How to download CSV using a href with a # (number sign) in Chrome?


I ran into this same problem, the only change that I did was keeping the "data:text/csv;charset=utf-8," unencoded and just endoding the CSV data portion and use encodeURIComponent instead of encodeURI like so:

let prefix = "data:text/csv;charset=utf-8,";let header = "Col1, Col2, Col3";let csvContent = header + "\r\n";csvContent += "ac, 123, info here" + "\r\n";csvContent += "dfe, 432, #2 I break" + "\r\n";csvContent += "fds, 544, I'm lost due to previous number sign";var encodedUri = prefix + encodeURIComponent(csvContent);var link = document.createElement("a");link.setAttribute("href", encodedUri);link.setAttribute("download", "file.csv");document.body.appendChild(link);link.click();

Copy and paste that into a Chrome console window and it works as expected.