下载

来源:互联网 发布:用友t3安装数据库 编辑:程序博客网 时间:2024/04/24 02:07

 

try
        {
            string pathName = "";
            string fileName = "Content.txt";
            string fullName = pathName + fileName;
            string FullFileName = Server.MapPath(fullName);
            FileInfo DownloadFile = new FileInfo(FullFileName);
            if (DownloadFile.Exists)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.ASCII));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.Write("<script>alert(''文件不存在!);</script>");
            }
        }
        catch
        {
            Response.Write("<script>alert(''文件不存在!);</script>");
        }

 

 

类中的方法
 public class DownLoad
    {
        public static void FileDownLoad(string pathName, string fileName)
        {
            try
            {
                string fullName = pathName + fileName;
                string FullFileName = System.Web.HttpContext.Current.Server.MapPath(fullName);
                FileInfo DownloadFile = new FileInfo(FullFileName);
                if (DownloadFile.Exists)
                {
                    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(fileName, System.Text.Encoding.ASCII));
                    HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                    HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                }
                else
                {
                    HttpContext.Current.Response.Write("<script>alert(''文件不存在!);</script>");
                }
            }
            catch
            {
                HttpContext.Current.Response.Write("<script>alert(''文件不存在!);</script>");
            }
        }
    }

 


DBUtility.DownLoad.FileDownLoad("~/", "Content.txt");
 

原创粉丝点击