I'm trying to save a CSV file using JavaScript, with a prepended UTF-8 BOM. However, when checking the downloaded file, it seems that the BOM is always stripped. The following code reproduces the issue:
var csv = '\ufefftest,test2';
var blob = new Blob([csv], {type: 'text/csv;charset=utf-8'});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'test.csv';
document.body.appendChild(a);
a.click();
Adding the BOM character to the string twice produces the correct result:
var csv = '\ufeff\ufefftest,test2';
The resulting file should have the BOM character at the beginning.
Why is it being stripped in this example?
EDIT: My use case is generating a CSV file, and ensuring that the file can be opened with correct encoding by Microsoft Excel. I'm thinking that maybe the BOM is detected and truncated, but Excel needs the character to be present to detect UTF-8.