string path = context.Request["path"];
string FileName = path.Substring(path.LastIndexOf("/") + 1);
//设置http输出流的类型
context.Response.ContentType = "application/octet-stream";
string userAgent = context.Request.UserAgent;
if (userAgent != null)
{
// 检查User-Agent字符串是否包含"MSIE"或"Trident"(某些版本的IE使用Trident作为渲染引擎)
if (userAgent.Contains("MSIE") || userAgent.Contains("Trident"))
{
//IE11的时候直接转
FileName = HttpUtility.UrlEncode(FileName, Encoding.UTF8);
context.Response.AddHeader("Content-Disposition", $"attachment; filename*=UTF-8''{FileName}");
}
else
{
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
}
}
//拼接虚拟路径,获取相对应的物理路径
string filename = context.Server.MapPath(path);
//http响应输出流,下载文件
context.Response.TransmitFile(filename);