日期:2014-05-17  浏览次数:20418 次

Response.WriteFile()出现乱码
我刚开学习asp
编写了下面才小程序
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="file59.aspx.cs" Inherits="file59" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="C#" runat="server">
  private void page_load(object sender, EventArgs e)
  {
  string strFilename = Server.MapPath(".") + "\\file.txt";
  Response.WriteFile(strFilename);
  } 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>无标题页</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <h4 align="center">writFile方法示例</h4> 
  </div>
  </form>
</body>
</html>

file.txt中随意的输入了一些汉字“这是一个测试文件来自file.txt”,可是运行结果为:??????????????????file.txt 
为什么汉字是乱码?
请大家帮忙


------解决方案--------------------
输出前这样设一下 Response.ContentEncoding = System.Text.Encoding.Default;
不然会以字节形式输出
------解决方案--------------------
C# code
string strFilename = Server.MapPath(".") + "\\file.txt"; 
        FileInfo DownloadFile = new FileInfo(strFilename); 
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.Charset = "utf-8";
        Response.ContentType = "application/msword";
        Response.AppendHeader("Content-Disposition",
                              "Attachment;FileName=" +
                              HttpUtility.UrlEncode("123.doc", Encoding.UTF8));
        Response.ContentEncoding = Encoding.UTF8;
        Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();