C# 指定物理目录下载文件,Response.End导致“正在中止线程”异常的问题
FileHandler http://www.cnblogs.com/vipsoft/p/3627709.html
UpdatePanel无法导出下载文件: http://www.cnblogs.com/vipsoft/p/3298299.html
//相对路径下载。path: ~/DownLoad/
//<add key="DownLoadPath" value="~/DownLoad/"/>
public static bool DownLoadFile(string path, string fileName)
{
bool result = false;
try
{
string filePath = HttpContext.Current.Server.MapPath(path + fileName);
if (File.Exists(filePath))
{
FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
result = true;
}
}
catch
{
}
return result;
} //物理路径下载。path: D:\DownLoad\
//<add key="DownLoadPath" value="D:\DownLoad\"/>
public static bool DownLoadFile(string path, string fileName)
{
bool result = false;
string filePath = path + fileName;
if (File.Exists(filePath))
{
try
{
//string filePath = HttpContext.Current.Server.MapPath(path + fileName); FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
result = true;
}
catch (Exception e)
{
}
finally
{
HttpContext.Current.Response.End(); //解决 ThreadAbortException 异常问题 }
}
return result;
}
两种方法的结合
public static bool DownLoadFile(string path, string fileName)
{
bool result = false;
string filePath = path + fileName;
if (File.Exists(filePath))
{
result = true;
}
else
{
try
{
filePath = HttpContext.Current.Server.MapPath(path + fileName);
if (File.Exists(filePath))
{
result = true;
}
}
catch
{
result = false;
}
}
if (result)
{
try
{
//string filePath = HttpContext.Current.Server.MapPath(path + fileName);
FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
}
catch (Exception e)
{
}
finally
{
HttpContext.Current.Response.End();
}
}
return result;
}
根据一些业务逻辑返回相应的状态字符串,如果出现异常做返回“error”,我预期它返回“状态1”,结果测试时发现
AJAX回调的结果是“状态1error”,它居然抛出异常了!
google后得知:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的
Application_EndRequest 事件,同时抛出ThreadAbortException 异常,异常信息为“正在中止线程”。另外
Response.Redirect、Server.Transfer方法也会出现这个问题,因为它们内部调用了Response.End 方法。
它给出的解决方案是使用HttpContext.Current.ApplicationInstance.CompleteRequest 方法以跳过
Application_EndRequest 事件的代码执行,但是我试了后发现虽然不抛出异常了,但是页面后面的代码依然会执行,
达不到Response.End的效果。
Response.End导致“正在中止线程”异常的问题,来源:
http://www.cnblogs.com/jintianhu/archive/2011/02/16/1952833.html
C# 指定物理目录下载文件,Response.End导致“正在中止线程”异常的问题的更多相关文章
- wget---从指定的URL下载文件
wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服务器打断下 ...
- 从指定的URL下载文件
通过使用URLDownLoadToFile函数,我们能从指定的URL下载文件,保存到本地,并且下载的文件类型可以是可执行文件 实例如下,http://www.xuexic.com 的根目录下存在一个l ...
- java web 下载文件 response.setHeader()的用法 (转载)
response.setHeader()的用法 response.setHeader()下载中文文件名乱码问题 收藏 1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但 ...
- JavaWeb下载文件response
以下代码在 chrome.firefox,安卓自带手机浏览器上测试通过,但未经过完全测试,先记录下 public static void downLoadFile(HttpServletRequest ...
- 爪哇国新游记之二十八----从url指定的地址下载文件到本地
package download; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; ...
- playwright--自动化(三): 跳过检测 使用正常谷歌 指定用户数据 下载文件
首先上一个被拷贝的惨不忍睹 上一个是滑块验证[https://www.cnblogs.com/carl-/p/15761861.html] 还是前两天做一个商城后台爬虫,限制用户缓存,不能用谷歌开发版 ...
- php curl下载文件由于空格导致下载文件失败
<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...
- Response.End ,Response.Redirect、Server.Transfer 引发 “正在中止线程”异常的问题
google后得知:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件,同时抛出ThreadAbortExcepti ...
- 使用Sparse Checkout 排除跟踪Git仓库中指定的目录或文件
应用场景 在一个大工程里包含由不同部门开发的模块时,项目的Git仓库肯定很大,造成每次Git操作相对比较耗时.因为开发人员一般只关心他们部门的模块的代码,所以完全可以排除一些他完全不需要用到的目录.这 ...
随机推荐
- Python的平凡之路(10)
异步IO 数据库 队列 缓存 1.Gevent协程 定义:用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下 ...
- java 自带md5加密
package test; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; pub ...
- nslog一些用法
1.nslog打印方法出来 NSLog(@"%@",NSStringFromSelector(_cmd)); 2.debug模式下打印一些信息,release模式下则不打印 #if ...
- iOS-Xcode7 网络连接
Xcode7 网络无法连接问题 1.The resource could not be loaded because the App Transport Security policy require ...
- mm/mmap.c
/* * linux/mm/mmap.c * * Written by obz. */#include <linux/stat.h>#include <linux/sched. ...
- bs结构socket(udp)通信
以前我所做的基于socket通信都是采用cs结构,现公司有一个项目需要在bs中反控设备,于是研究了一番,现将成果公布,方便以后查阅. 服务端: #region udp int recv; byte[] ...
- sql 查询效率
1. SQL优化的原则是:将一次操作需要读取的BLOCK数减到最低,即在最短的时间达到最大的数据吞吐量.调整不良SQL通常可以从以下几点切入: 检查不良的SQL,考虑其写法是否还有可优化内容 检查子查 ...
- 基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用
在前面介绍了两篇关于我的基于MVC4+EasyUI技术的Web开发框架的随笔,本篇继续介绍其中界面部分的一些使用知识,包括控件的赋值.取值.清空,以及相关的使用. 我们知道,一般Web界面包括的界面控 ...
- VB6对象与地址相互转换
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (lpDest As ...
- MVC 记录操作日志与过滤特殊字符
最近进行的MVC系统需要用到记录操作日志和过滤特殊字符的功能,如果每个action中都调用记录日志的方法就太麻烦了,所以根据需要结合mvc的过滤机制 写了个特殊字符验证与记录操作日志的公用类: pub ...