日期:2014-05-18  浏览次数:20497 次

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

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

另请教,我这里的文件是程序自动生成的,不存盘有没有办法能直接从内存中发送到客户端以附件下载??


------解决方案--------------------
我是这样做的没问题...
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();
------解决方案--------------------
如果是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();
------解决方案--------------------
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 ";
------解决方案--------------------
把文件内容写在MemoryStream里,在写的时候用Response.BinaryWrite(m.GetBuffer()),其中m是MemoryStream对象。我就是这样来生成pdf的,然后直接发到客户端的。