在服务器上生成文件的内容..给客户端直接下载.文件必需是unicode编码格式,代码.string fileName = "aaaa.csv";
StringWriter sw = new StringWriter();
sw.WriteLine("abs中文");
          
this.Response.Clear();
this.Response.Buffer = true;
this.Response.Filter.Close();
this.Response.ContentEncoding = System.Text.Encoding.Unicode;
this.Response.Charset = "Unicode";
this.Response.AddFileDependency(fileName);
this.Response.ContentType = "text/csv";
this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
this.Response.Write(sw);
this.Response.End();
sw.Close();
如果字符串里没有中文,下载下来的文件就是unicode编码的文件,如果有中文就不是,中文也变在乱码.请教这个要怎么解决?

解决方案 »

  1.   

    try this.Response.ContentEncoding = System.Text.Encoding.Default;or this.Response.ContentEncoding = System.Text.Encoding.UTF7;
      

  2.   

    再试试 UTF8 or GB2312
      

  3.   

    1,html页面,即aspx页面声明
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />2,html页面(aspx页面),及cs文件保存时采用的编码为utf-8这样,一般不会有问题.
      

  4.   

    修改webConfig
    内容如下
    system.web>
    <globalization fileEncoding="gb2312" requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" uiCulture="zh-CN"/>
    <!-- 
                    设置 compilation debug="true" 将调试符号插入
                已编译的页面中。但由于这会 
                影响性能,因此只在开发过程中将此值 
                设置为 true。
            -->
    <compilation debug="true">
      

  5.   

    还是不行..保存下来的还是ansi码文件
      

  6.   

    按我下面说的步骤建两个文件:
    1
    开始>>附件>>记事本>>随便敲几个字母,比如abcde.然后点保存,存为a.txt(默认为ansi)
    2
    开始>>附件>>记事本>>随便敲几个字母,比如abcde.然后点保存,不要急着保存,在保存对话框那里,编码那里选utf-8,再存为b.txt用记录本分别打开a.txt和b.txt你看到的内容是一样的.
    用ultra eidt等其它工具,以二进制方式查看文件,你看到的内容是不一样的.做完我上面说的, 再回头看我在4楼的回贴,你就明白我的意思了.