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文件記錄到的錯誤是”執行緒已終止“什麽的
求解!!!!!!
急!!!!!!!
--------------------编程问答-------------------- 帮顶! --------------------编程问答-------------------- 那你把下载另外弄一页 此页就提供链接 --------------------编程问答-------------------- HttpContext.Current.Response.End();
把这句话屏蔽掉。 --------------------编程问答--------------------
+1
Response.End();会抛异常的
补充:.NET技术 , ASP.NET