【发布时间】:2020-06-26 17:55:59
【问题描述】:
我将content_type 和to_csv encoding 都设置为utf_8_sig
response = HttpResponse(content_type='text/csv;charset=utf_8_sig')
response['Content-Disposition'] = 'attachment; filename=somefilename.csv'
df.to_csv(path_or_buf=response,sep=',',float_format='%.2f',index=False,decimal=",",encoding='utf_8_sig')
然后,
在 javascript 中发送 csv 给用户
//ajax response
DownlodCsv(response);
const DownloadCsv = (function() {
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function(data, fileName) {
const blob = new Blob([data], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
但它仍然是utf-8 而不是utf-8-sig
(因为我不能用excel打开这个)
有什么地方需要检查吗??????
【问题讨论】:
-
我猜 javascript 将响应标头设置为
octet/stream? -
我将它切换到 text/csv 但问题仍然存在...
标签: javascript python django unicode character-encoding