user3520259
user3520259

Reputation: 11

Export CSV in JavaScript Using UTF-8 and /uFEFF with dynamic CSV file name

Im trying to export String data to CSV using unicode in JavaScript using UTF-8 and \uFEFF. The problem is when the CSV file build he take the string after comma , and the first cell filled with this string : "Content-Disposition:attachment;filename="+filename+"\ ," " . I tried to change the position of the charset to the end, but the fileName must be in the end of the link.

Any ideas ?

thanks!

downloadCampaingnCsv: function(campaignIds,filename, url){

                  var s = new Server();
                  var response = s.sendJSON(ApiBaseUrl +  url,'POST',campaignIds);
                  var csvContent = "data:text/csv;charset=utf-8,/uFEFF;Content-Disposition:attachment;filename="+filename+"\ ," ;     
                  if (response != "No Data!"){
                    var encodedUri = csvContent +encodeURI(response); 
                  } else {
                    var encodedUri  = "data:text/csv;filename="+filename+"\ ,"  + encodeURI(response);
                  }
                  var link = document.createElement("a");
                  link.setAttribute("href", encodedUri);
                  link.setAttribute("download", filename);
                  link.click();
   }

Upvotes: 1

Views: 8878

Answers (1)

user2991975
user2991975

Reputation: 21

the answer is in this link Javascript to csv export encoding issue the bottom line is "data:attachment/csv;charset=utf-8,%EF%BB%BF" + {{yuourCsvString}};

Upvotes: 2

Related Questions