文件下载,附件的中文名称乱码

falleaves 2007-06-25 09:45:50
我的程序中要自动生成文件下载,但下载以中文文件名会在下载确认窗体上显示乱码,英文文件名就不会,不知道如何解决,望高手指点。

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();

另请教,我这里的文件是程序自动生成的,不存盘有没有办法能直接从内存中发送到客户端以附件下载??
...全文
1036 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
hph201411 2008-04-28
  • 打赏
  • 举报
回复
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
lyq708 2007-10-22
  • 打赏
  • 举报
回复
[u][/
u]
hmg43 2007-07-20
  • 打赏
  • 举报
回复
高手解释以下上面的代码具体用法?
hmg43 2007-07-16
  • 打赏
  • 举报
回复
我的代码:
Response.Clear()
Response.Charset = "GB2312"
Response.ContentEncoding = System.Text.Encoding.UTF8
' 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment filename=" + Server.UrlEncode("name.htm"))
' 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString())

' 默认为text/html方式
Response.ContentType = "application/ms-html"

Response.Write(strContent)

strContent为字符串变量,内容为一个完整的Html文件。
运行后发现将保存的内容包含当前ASPX页面内容,及Html内容+当前ASPX页面内容。
而用Response.WriteFile(file)替换Response.Write(strContent)后一切正常。
hmg43 2007-07-10
  • 打赏
  • 举报
回复
受到启发!请问怎么代码生成PDF文件?
falleaves 2007-06-29
  • 打赏
  • 举报
回复
代码生成的文件在内存中,不存盘就能发送到客户端,没有人能解决呀????
涛声宜旧 2007-06-29
  • 打赏
  • 举报
回复
Encoding.GetEncoding("utf-8")
tujiaping 2007-06-29
  • 打赏
  • 举报
回复
把文件内容写在MemoryStream里,在写的时候用Response.BinaryWrite(m.GetBuffer()),其中m是MemoryStream对象。我就是这样来生成pdf的,然后直接发到客户端的。
sweet12345 2007-06-25
  • 打赏
  • 举报
回复
string FileName = myPageName + ".xls";

response.Clear();
response.Buffer = true;
response.Charset = "utf-8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
response.ContentType = "application/ms-excel";
bdbox@qq.com 2007-06-25
  • 打赏
  • 举报
回复
编码一下文件名称。

我的e-mail:bdbox@163.com,欢迎与我交流。
SassyBoy 2007-06-25
  • 打赏
  • 举报
回复
能解决问题就好。
falleaves 2007-06-25
  • 打赏
  • 举报
回复
谢谢大家的帮助,中文文件名已解决。

我导出Excel是通过Office提供的接口支持,通过Excel.Application生成Excel的,完全自己编码,不是通过DataGrid或DataList等控件的RenderControl方法导出的,生成后有个Sheets.SavaAs方法可以存盘,但我想不存盘就输出至客户端,因为输出到客户端之后,存盘的文件也就没用了,我现在是通过输出到客户端后在把文件删除来解决,但我觉得这样会多一次存盘又读取的过程,想有没有办法不存盘就直接输出到客户端。
SassyBoy 2007-06-25
  • 打赏
  • 举报
回复
如果是datagrid或者gridview这类就这样试试

Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
this.EnableViewState = false;

//定义输入流

System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter txtwriter = new HtmlTextWriter(writer);
//将DataGrid中的内容输出到txtwriter流中
this.dgList.RenderControl(txtwriter);

//作为附件输出,filename=ReqExcel.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc|.xls|.txt|.htm  
Response.ContentType = "application/ms-excel"; //ContentType指定文件类型 可以为application/ms-excel   
//application/ms-word | application/ms-txt | application/ms-html 或其他浏览器可直接支持文档 

Response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls"); //下载
//Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");  
Response.Write(writer);

Response.End();
Alvin709 2007-06-25
  • 打赏
  • 举报
回复
我是这样做的没问题...
Response.Charset="GB-2312";
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(filename+".xls"));
Response.ContentEncoding=System.Text.Encoding.UTF7;
Response.ContentType="application/ms-excel";
Dat_Xianshi.Page.EnableViewState=false;
System.IO.StringWriter tw=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw=new HtmlTextWriter(tw);
Dat_Xianshi.RenderControl(hw);
Response.Write(""+tw.ToString()+"");
Response.End();
DavidNoWay 2007-06-25
  • 打赏
  • 举报
回复
SassyBoy 2007-06-25
  • 打赏
  • 举报
回复
你是用什么控件导出excel的?
falleaves 2007-06-25
  • 打赏
  • 举报
回复
加上Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");  
再试试看。
--------------------------------------

这个试过,不行
SassyBoy 2007-06-25
  • 打赏
  • 举报
回复
lz你是想直接给用户下载xls吧?
SassyBoy 2007-06-25
  • 打赏
  • 举报
回复
加上Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");  
再试试看。
huangbznet 2007-06-25
  • 打赏
  • 举报
回复
过来看一下,怎么回事
加载更多回复(1)

62,040

社区成员

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

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

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

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