I am having a problem generating and downloading a text file in UTF8 that includes an emoji. The problem is, when I download the file that includes and emoji, the generated file is not encoded in UTF8 and the emoji is not shown correctly.
I've used this solution to generate and download the file I need. This is the code I use:
function download(filename, text) {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
So, if I use it like this:
downloadFile('withoutEmoji.txt','This is a test without emoji');
It downloads a file in UTF8.
But, when I use it like this:
downloadFile('withEmoji.txt','This is a test with emoji ');
The file I download doesn't show the emoji correctly, and the encoding of the file is no longer UTF8.
If I convert the 'withEmoji.txt' file to UTF8 (using notepad++ for example) the emoji gets shown correctly in the file.
How can I force the file or text to be UTF8? or is there a way to convert the emoji before generating the file? I need the file to include the emoji, and to be in UTF8.
You can see this behaviour in this fiddle.
EDIT
Notepad++ recognises the 'withEmoji.txt' file with ANSI encoding. Vanilla notepad recognises the file with 'UTF8' encoding. Using this service the file gets recognised as "File Type: ASCII text, with no line terminators".