漫天 flurrying

漫天 Asp.net C# flurrying 程序员 SEO asp java jsp javascript
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 文件下载方法

Posted on 2010-08-10 17:52  漫天  阅读(310)  评论(0编辑  收藏  举报
 public void FileDownload(string FileName)
        {
            try
            {
                string FullFileName = HttpContext.Current.Server.MapPath(FileName);
                FileInfo DownloadFile = new FileInfo(FullFileName);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = false;
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            catch (Exception exc) {
                Response.Write("<script>alert('下载失败。错误信息:" + exc.Message + "');</script>");
            }
        }