求助:.NET Web Form应用在Edge 103.x版本下载缓慢问题
结合你描述的现象(仅Edge 103.x下载慢,旧版本和其他内网应用正常),大概率是你的下载响应头或代码细节与Edge新版本的兼容性问题,以下是针对性的排查和修复方案:
1. 补充Content-Length响应头
你的前三个下载代码块都没有设置Content-Length,Edge 103+对于没有明确内容长度的响应,可能会启用分块传输编码(Chunked),部分场景下会导致客户端等待逻辑变慢。
修复示例:
针对字节数组写法:
Response.AddHeader("Content-disposition", "attachment; filename=foo.txt"); Response.ContentType = "application/octet-stream"; // 添加Content-Length头 Response.AddHeader("Content-Length", myContentByteArray.Length.ToString()); Response.BinaryWrite(myContentByteArray); Response.End();
针对内存流写法:
Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-disposition", "attachment; filename=foo.pdf"); // 用MemoryStream的实际长度,而非GetBuffer的总缓冲区长度 long contentLength = myMemoryStream.Length; Response.AddHeader("Content-Length", contentLength.ToString()); // 推荐用ToArray()避免传输空字节,或者直接CopyTo Response.OutputStream.Write(myMemoryStream.ToArray(), 0, (int)contentLength); Response.OutputStream.Flush(); Response.End();
2. 修正内存流缓冲区的错误使用
你的第三个代码块中,myMemoryStream.GetBuffer().Length会返回整个分配的缓冲区大小(包含未使用的空字节),而非实际内容长度,这会导致传输多余的无效数据,Edge 103+对这类冗余数据的处理可能更耗时。
修复方式:
- 替换
myMemoryStream.GetBuffer()为myMemoryStream.ToArray(),只传输实际内容 - 或者使用
myMemoryStream.Length作为写入长度,同时用myMemoryStream.Position = 0重置流指针后,调用myMemoryStream.CopyTo(Response.OutputStream)更高效
3. 清理重复/错误的Content-Type设置
第四个代码块中重复设置了Content-Type,且application/Excel不是标准MIME类型(正确的Excel格式应该是application/vnd.ms-excel或application/vnd.openxmlformats-officedocument.spreadsheetml.sheet针对xlsx),这种不规范的头可能导致Edge新版本的MIME解析逻辑异常。
修复示例:
Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); // 统一使用标准MIME类型 Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-disposition", "attachment; filename=foo.xls"); Response.AddHeader("Content-Length", myFile.Length.ToString()); Response.WriteFile(myFile.FullName); Response.End();
4. 替换Response.End()为CompleteRequest()
Response.End()会强制中止当前线程,可能导致响应流未完全刷新,Edge 103+对不完整响应的容错性可能更低。可以改用HttpContext.Current.ApplicationInstance.CompleteRequest()来结束请求,避免线程中止异常。
替换示例:
// 替换Response.End(); HttpContext.Current.ApplicationInstance.CompleteRequest();
5. 排除客户端因素(验证用)
虽然其他内网应用正常,但可以快速验证是否是Edge本地配置问题:
- 用Edge的InPrivate窗口测试下载,排除缓存或扩展干扰
- 临时关闭Edge的SmartScreen筛选器(设置>隐私、搜索和服务>Windows安全中心>SmartScreen筛选器),测试是否是安全扫描导致的延迟
内容的提问来源于stack exchange,提问作者Saledan

