当前位置: 代码迷 >> ASP.NET >> Response.WriteFile()出现乱码解决思路
  详细解决方案

Response.WriteFile()出现乱码解决思路

热度:2986   发布时间:2013-02-25 00:00:00.0
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();
  相关解决方案