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操作相对比较耗时.因为开发人员一般只关心他们部门的模块的代码,所以完全可以排除一些他完全不需要用到的目录.这 ...
随机推荐
- (C语言)结构体成员的引用->(箭头)和 .(点)
关于结构体成员的引用有这样的规律: 箭头(->):左边必须为指针: 点号(.):左边必须为实体. 那么如果一个结构体指针引用一个成员,这个成员又是一个结构体(并且是一个实体),那么如果要引用这个 ...
- sqoop将关系型的数据库得数据导入到hbase中
1.sqoop将关系数据库导入到hbase的参数说明
- c++语法随身记
1.memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的 ...
- Context的使用(转)
1.Context概念 Context,相信不管是第一天开发Android,还是开发Android的各种老鸟,对于Context的使用一定不陌生~~你在加载资源.启动一个新的Activity.获取系统 ...
- iOS开发ARC内存管理
本文的主要内容: ARC的本质 ARC的开启与关闭 ARC的修饰符 ARC与Block ARC与Toll-Free Bridging ARC的本质 ARC是编译器(时)特性,而不是运行时特性,更不是垃 ...
- crontab 移动日志-超越昨天的自己系列(12)
linux上定时执行某些脚本是管理服务器的时候比较常用的场景,比如定时检查进程是否存在,定时启动或关闭进程,定时检查日志删除日志等. 当我打开google百度crontab时长篇大论的一大堆,详细解释 ...
- GDB中文手册
用GDB调试程序GDB概述 2使用GDB 5GDB中运行UNIX的shell程序 8在GDB中运行程序 8调试已运行的程序 两种方法: 9暂停 / 恢复程序运行 9一.设置断点(BreakPoint) ...
- byte与char的区别
byte 是字节数据类型 ,是有符号型的,占1 个字节:大小范围为-128—127 .char 是字符数据类型 ,是无符号型的,占2字节(Unicode码 ):大小范围 是0—65535 :char ...
- Spring MVC 4.2 增加 CORS 支持 Cross-Origin
基于XML的配置: <mvc:cors> <mvc:mapping path="/api/**" allowed-origins="http://dom ...
- java空指针异常 for循环时,使用了值为null的变量
错误代码: for(Map<String,Object> videoItemMap:learnVideoList){ String videoStuId=MapUtils.getStrin ...