asp.net 下載異常 “執行緒已終止”

try
{
string FilePath = "";
if (path.Contains("~"))
{
FilePath = HttpContext.Current.Request.MapPath(path);
}
else
{
FilePath = path;
}
System.IO.FileStream r = new System.IO.FileStream(FilePath, System.IO.FileMode.Open);
//设置基本信息
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
HttpContext.Current.Response.ContentType = "application/octet-stream;";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.Charset = "utf-8";
//HttpUtility.UrlEncode解决中文文件名乱码问题
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName).Replace("+", "%20"));
HttpContext.Current.Response.AddHeader("Content-Length", r.Length.ToString());
try
{
while (true)
{
//开辟缓冲区空间
byte[] buffer = new byte[1024];
//读取文件的数据
int leng = r.Read(buffer, 0, 1024);
if (leng == 0)//到文件尾,结束
break;
if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入
HttpContext.Current.Response.BinaryWrite(buffer);
else
{
//读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
byte[] b = new byte[leng];
for (int i = 0; i < leng; i++)
b[i] = buffer[i];
HttpContext.Current.Response.BinaryWrite(b);
}
}
r.Close();//关闭下载文件
HttpContext.Current.Response.End();//结束文件下载
}
catch (Exception ex)
{
string[] strMsg = { "Message:" + ex.Message, "Source:" + ex.Source, "StackTrace:" + ex.StackTrace };
fileHandling("File", strMsg);
return false;
}
}
catch (Exception ex)
{
string[] strMsg = { "Message:" + ex.Message, "Source:" + ex.Source, "StackTrace:" + ex.StackTrace };
fileHandling("File", strMsg);
return false;
}
return true;
這是我的下載代碼,本地調試時沒有問題,可以下載,可是放到服務上,點擊鏈接下載時沒有反應,我的log文件記錄到的錯誤是”執行緒已終止“什麽的
求解!!!!!!
急!!!!!!!
相关推荐
[ASP.NET]web实现用FTP上传、下载文件
[ASP.NET]web实现用FTP上传、下载文件。对应博客文章: https://blog.csdn.net/djk8888/article/details/80736524
ASP.NET 大文件下载实战:WriteFile、TransmitFile、BinaryWrite 对比与断点续传实现
/// /// WriteFile实现下载 /// /// 文件绝对路径 /// 文件名称 public static void DownLoadWriteFile(string AbsPath, string fileName) { try { FileInfo file
asp.net多文件下载 MVC多文件下载
asp.net多文件下载 MVC多文件下载 仅供参考
ASP.NET Core 全局异常处理和日志记录
文章目录添加自定义业务异常类添加全局异常过滤器像容器添加服务 添加自定义业务异常类 public class BusinessException : Exception { public BusinessException(){} public BusinessException(string message) : base(message) {} public BusinessException(string message, Exception innerException) : base(message, innerException) {} }
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
如果在 Microsoft .NET Framework 2.0 上构建的基于 Microsoft ASP.NET 的应用程序中引发未处理的异常,该应用程序将会意外退出。如果出现这个问题,不会在应用程序日志中记录了解此问题所必需的异常信息。


