下载编码ServiceStack的CSV文件(UTF 8)

时间:2015-02-03 10:34:35

标签: csv utf-8 servicestack

我是ServiceStack的新手。它具有为数据提供csv文件的功能,但我需要以UTF8格式下载它,因为我得到了一些特殊字符。我试过这个配置设置。

SetConfig(new HostConfig
        {
            AppendUtf8CharsetOnContentTypes = new HashSet<string> { MimeTypes.Csv } 
        });

但没有运气..请说明我做错了什么。

2 个答案:

答案 0 :(得分:2)

默认情况下,ServiceStack文本序列化程序已经序列化为UTF8。以下配置仅附加UTF8后缀,因此返回的完整Content-Type为text/csv; charset=utf-8

SetConfig(new HostConfig {
    AppendUtf8CharsetOnContentTypes = new HashSet<string> { MimeTypes.Csv } 
});

它并没有改变已经序列化为UTF-8的内容。

我做了一项更改,允许您修改用于ServiceStack.Text in this commit中不同文本序列化程序的UTF8Encoding。

这将允许您指定为CSV序列化器发出UT8 BOM:

CsvSerializer.UTF8Encoding = UTF8Encoding(true);

此更改可从 v4.0.37 + 获得,现在为available on MyGet

答案 1 :(得分:1)

对于ServiceStack版本&gt; 5.x.x你必须像这样改变你的代码

CsvSerializer.UseEncoding = PclExport.Instance.GetUTF8Encoding(true);

默认使用CsvSerializer.UseEncoding = PclExport.Instance.GetUTF8Encoding(false); // without BOM