C#下载文件

来源:互联网 发布:土木工程预算软件 编辑:程序博客网 时间:2024/05/29 17:26
        string path = Server.MapPath("../../Upload/Template/模版.doc");
        if (!File.Exists(path))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('模版文件不存在!');</script>");
            return;
        }
        else
        {
            //下载该模版文件
            FileInfo file = new FileInfo(path);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
            Response.AddHeader("Content-length", file.Length.ToString());
            Response.ContentType = "appliction/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }