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操作相对比较耗时.因为开发人员一般只关心他们部门的模块的代码,所以完全可以排除一些他完全不需要用到的目录.这 ...
随机推荐
- ubuntu下firefox浏览器flash player插件的安装
自从装了双系统后,ubuntu下的音乐软件只能选择网页播放器了,这无疑是需要播放插件的,这个插件就是falsh player. 当初使用usb启动盘安装的,在安装的过程中还会报找不到CD-rom的错, ...
- excel列递增方法技巧
最近处理世界买家网100多万的数据,需要用到excel来对数据进行处理,其中有一项是对数据做一个排序,以方便数据导入时来对应唯一的id编号,之前都是 几万,最多也是30来万的数据,所以列递增就直接用鼠 ...
- wordpress的备份与还原
在目录下创建一个文件来备份sql mysqldump -uroot -p '数据库名称'> 到 目录下创建的备份文件 然后输入密码 OK. 还原wordpress mysqldump -uro ...
- wordpress的创建
1.将mysql的安装文件放入虚拟机 2.搭建yum库 3.依次安装mysql的5个文件 最后一个server需要的依赖太多,所以用yum进行安装. 或者直接全部用yum进行安装 6.进行mysql的 ...
- 另一种图片上传 jquery.fileupload.js
今天遇到另外一种上传图片方法 用jquery.fileupload.js <input type="file" name="file[]" multipl ...
- 实际项目中积累的一些关于事件的简单应用JS代码段(能力有限,不喜轻喷,23333)
1:鼠标移入移出显示另一张图片 var yuanquan_1 = document.getElementById("yuanquan_1" ); yuanquan_1. onmo ...
- Neo4j Index Notes
Motivation GraphDatabasesBook: Robinson I., Webber J., Eifrem E. Graph Databases. 2013. 这本该是入门概念性质的书 ...
- yii使用MongoDB作为数据库服务软件[win7环境下](1)
1.进入http://php.net,在站内搜索栏搜索mongodb,查看相关的安装步骤信息. 2.找到相应的php.ini配置文件,使用wampserver等服务器软件时,千万不要找错了php.in ...
- QString转换为char* (转)
Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...
- Scrum2.0 项目基本完成