ASP.NET 资料下载

2018-06-23 23:12:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 1     public void downloadfile(string s_fileName)
 2     {
 3         HttpContext.Current.Response.ContentType = "application/ms-download";
 4         string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
 5         System.IO.FileInfo file = new System.IO.FileInfo(s_path);
 6         HttpContext.Current.Response.Clear();
 7         HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
 8         HttpContext.Current.Response.Charset = "utf-8";
 9 
10         string filename = file.Name;
11         string userAgent = HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower();
12         //处理firefox保存时文件名乱码的问题  
13         if (userAgent.IndexOf("firefox") == -1)
14             filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
15 
16         HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
17         HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
18         HttpContext.Current.Response.WriteFile(file.FullName);
19         HttpContext.Current.Response.Flush();
20         HttpContext.Current.Response.Clear();
21         HttpContext.Current.Response.End();
22     }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【工具】今天有人问我可以直接离线一个完整的网站吗?有没有什么

下一篇:MFC-简单的函数使用