关于asp.net 下载文件有中文出现乱码的问题

hebaobao19880921 2011-07-22 10:05:47

protected void FN_ResponseFile()
{
string id = Request["id"].ToString();
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
string path = bf.FN_SerchPathByFileId(guid).Tables["ds"].Rows[0]["FilePath"].ToString();//这是从数据库取的完整路径 .
int length;
long dataToRead;
string filename = System.IO.Path.GetFileName(path);
try
{
if (Request.UserAgent.Contains("MSIE") || Request.UserAgent.Contains("msie"))
{
filename = ToHexString(filename);
}
iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length; Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.GetEncoding("GB2312")));
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write("文件下载时出现错误!");
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}
/// <summary>
/// 为字符串中的非英文字符编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}

return builder.ToString();
}

/// <summary>
///指定 一个字符是否应该被编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";

if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;

return true;
}

/// <summary>
/// 为非英文字符串编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}


在网上找到了这个解决办法 不知道为什么还是乱码- -
...全文
172 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2011-07-22
  • 打赏
  • 举报
回复
文件名的处理方法

String ExportFileName = "中文文件名";
if (Request.Browser.Browser.IndexOf("MSIE") > -1)
{
ExportFileName = Server.UrlEncode(ExportFileName);
}

Response.AddHeader("Content-Disposition", "attachment; filename=" + ExportFileName );
孟子E章 2011-07-22
  • 打赏
  • 举报
回复
是内容乱还是文件名乱?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧