附件下载,中文文件名乱码如何解决???

falleaves 2007-06-25 09:36:43
我写了个程序,里面有文件要作为附件下载,英文文件名都正常,但中文文件名就会乱码,不知道如何解决,望高手指点。

代码如下:
=============================
Response.Clear();
Response.BufferOutput=true;
Response.Charset="utf-8";//此处用“GB2312”也不行
Response.AppendHeader("Content-Disposition","attachment;filename=测试.xls");
Response.ContentType = "application/vnd.ms-excel";
FileInfo mf=new FileInfo(sFile);
FileStream fs=mf.OpenRead();
Response.WriteFile(fs.Handle,0,mf.Length);
fs.Close();
...全文
799 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
falleaves 2007-06-25
  • 打赏
  • 举报
回复
中文文件名的问题已解决,谢谢大家的帮忙。
xiaotupansy 2007-06-25
  • 打赏
  • 举报
回复
再给你回复一次,我faint

string pathfile = @"F:\新建文件夹\1.txt"; //pathfile 是要下载的文件名称
System.IO.FileInfo file = new System.IO.FileInfo(pathfile);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
yangjia21_2007 2007-06-25
  • 打赏
  • 举报
回复
编码问题
yangjia21_2007 2007-06-25
  • 打赏
  • 举报
回复
这样写就OK了
Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(测试.xls));
huangbznet 2007-06-25
  • 打赏
  • 举报
回复
GB2312也不行么?,实在不行你重新写一下试试
huanglx 2007-06-25
  • 打赏
  • 举报
回复
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));

Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
xray2005 2007-06-25
  • 打赏
  • 举报
回复
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();

62,046

社区成员

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

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

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

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