html5csv icon indicating copy to clipboard operation
html5csv copied to clipboard

UTF8 Problem

Open MrTantum opened this issue 7 years ago • 2 comments

When I export something on my pc the file is not saved in UTF8 format. This results in problems when opening the files in notepad++.

I fixed this by adding "\ufeff" to the blob file and the a.href when doing the final export steps.

Maybe you can add some property to choose if the file is saved in utf-8 format.

// try IE solution first
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    try {
	var blob = new Blob(
	    ["\ufeff", decodeURIComponent(encodeURI(csvString))], {
		type: "text/csv;charset=utf-8;"
	    });
	navigator.msSaveBlob(blob, fname);
    } catch(e){ 
	errormsg = "error on CSV.download, IE blob branch:"+e;
	console.log(errormsg);
	if (strict) throw errormsg; 
    }
} else {
    // try Firefox/Chrome solution here
    try {
	var a = document.createElement('a');
	if (!('download' in a)) throw "a does not support download";
	a.href = 'data:attachment/csv,'+ "\ufeff" +encodeURIComponent(csvString);
	a.target = '_blank';
	// use class instead of id here -- PJB 2015.01.10
	a.class = 'dataURLdownloader';
	a.download = fname;
	document.body.appendChild(a);
	a.click();
    } catch(e){
	errormsg = "error on CSV.download, data url branch:"+e;
	console.log(errormsg);
	if (strict) throw errormsg; 
    }

MrTantum avatar Sep 05 '17 13:09 MrTantum

Thank you for this report.

Do you have

<META CHARSET="UTF-8" /> 

to set UTF-8 encoding globally on web page, in the <HEAD> of the webpage?

Is your webpage in a non-English language, where other encoding is required to display properly?

DrPaulBrewer avatar Sep 05 '17 22:09 DrPaulBrewer

No I can not ensure the header is added to every page. The code is loaded on demand after page load.

Yes the webpage is multi language.

MrTantum avatar Sep 19 '17 08:09 MrTantum