使用UTF-8和/ uFEFF以动态CSV文件名导出CSV

时间:2014-12-21 09:33:55

标签: javascript csv utf-8

我尝试使用UTF-8和\ uFEFF在JavaScript中使用unicode将String数据导出为CSV。 问题是当CSV文件构建时,他在逗号后面接受字符串,第一个单元格填充了这个字符串:“Content-Disposition:attachment; filename =”+ filename +“\,”“。 我试图将字符集的位置更改为结尾,但fileName必须位于链接的末尾。

有什么想法吗?

谢谢!

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();
   }

1 个答案:

答案 0 :(得分:2)

答案就在这个链接中 Javascript to csv export encoding issue 底线是"数据:attachment / csv; charset = utf-8,%EF%BB%BF" + {{yuourCsvString}};