Search code examples
javascriptcsvexport-to-csv

Javascript encodeURI not work well with csv


I'm trying generate short CSV by the code bellow :

Source

const csv_content = `"Hello"þ"World"þ"#"þ"123"`;
const href = 'data:attachment/csv;charset=utf-8,' + encodeURI(csv_content);
console.log(href);

I copy/past the href value in my browser and download it.

Value of href

data:attachment/csv;charset=utf-8,%22Hello%22%C3%BE%22World%22%C3%BE%22#%22%C3%BE%22123%22

I open it in Excel but I can't show any chars after # char and the # itself. If someone can explain me why encodeURI and # return me this issue, that would be wonderfull !

SOLUTION

evolutionxbox comment resolve this issue, you need use encodeURIComponent instead encodeURI

// encodes characters such as ?,=,/,&,:
console.log(`?x=${encodeURIComponent('test?')}`);
// expected output: "?x=test%3F"

Solution

  • SOLUTION

    evolutionxbox comment resolve this issue, you need use encodeURIComponent instead encodeURI

    // encodes characters such as ?,=,/,&,:
    console.log(`?x=${encodeURIComponent('test?')}`);
    // expected output: "?x=test%3F"