您的位置:首页 > 编程语言 > ASP

asp.net后台下载文件(解决中文乱码)

2013-03-14 12:55 423 查看
你是否在asp.net开发中遇到过下载文件时乱码的现象,也许这个代码不是最好的,但是它能帮你下载文件并且解决中文乱码的问题

private void DownLoadFile(string fileName)

{
string filePath = Server.MapPath(".") + "\\" + fileName;
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
本文出自 “蜗牛” 博客,请务必保留此出处http://sccassiel.blog.51cto.com/5398709/1154001
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: