KiMoGiGi 技术文集

不在乎选择什么,而在乎坚持多久……

IT博客 首页 联系 聚合 管理
  185 Posts :: 14 Stories :: 48 Comments :: 0 Trackbacks

  public static void ReturnHTTPStream(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);

            //HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fi.Name);
            HttpContext.Current.Response.AddHeader("Content-Length", fi.Length.ToString());
            HttpContext.Current.Response.AppendHeader("Last-Modified", fi.LastWriteTime.ToFileTime().ToString());
            HttpContext.Current.Response.AppendHeader("Location", HttpContext.Current.Request.Url.AbsoluteUri);

            HttpContext.Current.Response.ContentType = GetResponseContentType(fi.Extension);

            HttpContext.Current.Response.WriteFile(filePath);
            HttpContext.Current.Response.End();
        }
        private static string GetResponseContentType(string fileType)
        {
            string result;
            switch (fileType.ToLower())
            {
                case ".doc": result = "application/msword"; break;
                case ".xls":
                case ".xlt": result = "application/msexcel"; break;
                case ".txt": result = "text/plain"; break;
                case ".pdf": result = "application/pdf"; break;
                case ".ppt": result = "appication/powerpoint"; break;
                default: result = "application/unknown"; break;
            }
            return result;
        }

posted on 2006-10-15 11:09 KiMoGiGi 阅读(617) 评论(1)  编辑 收藏 引用 所属分类: Web小技巧

评论

# re: 下载功能代码 2009-08-14 08:34 shruti
在jsp中怎么实现下载功能  回复  更多评论
  

只有注册用户登录后才能发表评论。