C# 文件下载方法

 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>");
            }
        }

你可能感兴趣的:(文件下载)