日期:2014-05-19  浏览次数:20524 次

关于文件输出的问题
HttpPostedFile将一个文件上传到数据库中,截取路径最后 "\ "后的文件名,也存入数据库,输出文件时用:
Response.AppendHeader( "Content-Disposition ",   "attachment;filename= "   +   HttpUtility.UrlEncode((string)reader[ "aname "],   System.Text.Encoding.UTF8));
如果文件名是 "新建   文本文档.txt ",下载输出时就变成了 "新建+文本文档.txt ";
怎么回事?

------解决方案--------------------
System.Text.Encoding.UTF8-> Encoding.Default看看
------解决方案--------------------
因为你用了UrlEncode

而空格UrlEncode以后就是+


------解决方案--------------------
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
Page.Response.Clear();
Page.Response.ClearContent();
Page.Response.ClearHeaders();
Page.Response.AddHeader( "Content-Disposition ", "attachment;filename= " + Page.Server.UrlPathEncode(fileInfo.Name));
Page.Response.AddHeader( "Content-Length ", fileInfo.Length.ToString());
Page.Response.AddHeader( "Content-Transfer-Encoding ", "binary ");
Page.Response.ContentType = "application/octet-stream ";
Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding( "utf-8 ");
Page.Response.WriteFile(fileInfo.FullName);
Page.Response.Flush();
Page.Response.End();
------解决方案--------------------
UrlEncode就是这样啊,因为在Url的QueryString中 "+ "就是表示 " "。你可以选择:
1.照用UrlEncode,然后将 "+ "替换回来。
2.自己用Encoding写一个函数实现UTF-8的编码。