Response.WriteFile实现文件下载

需引入命名空间

using System.IO;

/// 
    /// Response.AddHeader实现下载
    /// 
    /// 完整的文件路径
    /// 文件名
    private void DownFile(string filePath, string fileName)
    {
        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();
    }

调用方法:

DownFile(Server.MapPath("~/abc.xml"), "abc.xml");

你可能感兴趣的:((C#)文件管理与上传)