c#下载文件案例

时间:2021-10-30 21:35:44

public static void HttpDown(string fileName, System.Web.UI.Page p_Page,string floder)
{
string path = fileName;
//输出到客户端
FileInfo file = new FileInfo(path);
p_Page.Response.Clear();
p_Page.Response.Charset = "GB2312";
p_Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//添加头信息,为"文件下载/另存为"对话框指定默认文件名
p_Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + p_Page.Server.UrlEncode(file.Name));
//添加头信息,指定文件大小,让浏览器能够显示下载进度
p_Page.Response.AddHeader("Content-Length", file.Length.ToString());
//指定返回的是一个不能被客户端读取的流,必须被下载
p_Page.Response.ContentType = "application/ms-excel";
//把文件流发送到客户端
p_Page.Response.WriteFile(file.FullName);
p_Page.Response.Flush();
Directory.Delete(floder, true);
}