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导致“正在中止线程”异常的问题的更多相关文章

  1. wget---从指定的URL下载文件

    wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服务器打断下 ...

  2. 从指定的URL下载文件

    通过使用URLDownLoadToFile函数,我们能从指定的URL下载文件,保存到本地,并且下载的文件类型可以是可执行文件 实例如下,http://www.xuexic.com 的根目录下存在一个l ...

  3. java web 下载文件 response.setHeader()的用法 (转载)

    response.setHeader()的用法 response.setHeader()下载中文文件名乱码问题 收藏 1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但 ...

  4. JavaWeb下载文件response

    以下代码在 chrome.firefox,安卓自带手机浏览器上测试通过,但未经过完全测试,先记录下 public static void downLoadFile(HttpServletRequest ...

  5. 爪哇国新游记之二十八----从url指定的地址下载文件到本地

    package download; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; ...

  6. playwright--自动化(三): 跳过检测 使用正常谷歌 指定用户数据 下载文件

    首先上一个被拷贝的惨不忍睹 上一个是滑块验证[https://www.cnblogs.com/carl-/p/15761861.html] 还是前两天做一个商城后台爬虫,限制用户缓存,不能用谷歌开发版 ...

  7. php curl下载文件由于空格导致下载文件失败

    <?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...

  8. Response.End ,Response.Redirect、Server.Transfer 引发 “正在中止线程”异常的问题

    google后得知:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件,同时抛出ThreadAbortExcepti ...

  9. 使用Sparse Checkout 排除跟踪Git仓库中指定的目录或文件

    应用场景 在一个大工程里包含由不同部门开发的模块时,项目的Git仓库肯定很大,造成每次Git操作相对比较耗时.因为开发人员一般只关心他们部门的模块的代码,所以完全可以排除一些他完全不需要用到的目录.这 ...

随机推荐

  1. springmvc学习第三天

    利用spring mvc 实现crud 1.导入jar包 commons-logging-1.2.jarjstl.jarspring-aop-4.1.6.RELEASE.jarspring-beans ...

  2. O_NONBLOCK on regular file

    It seems that writes/reads to regular files can't not be made non-blocking. I found the following re ...

  3. python模块之os

    os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说:绝对路径,父目录-- os.sep可以取代操作系统特定的路径分隔符.windows下为 "\ ...

  4. Javascript 事件对象(二)event事件

    Event事件: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  5. SVG裁剪和平移的顺序

    SVG 里为元素添加 clip-path 属性即可做出裁剪效果,添加 transfrom 属性可以平移.旋转元素. 根据需求不同,有两种情况: 先裁剪元素,再把裁剪后的图形平移 先平移元素,再按区域裁 ...

  6. Java-->Gson序列化及反序列化

    --> 首先导入jar包,并添加到Build Path --> 需要User类:有属性.构造方法和setter.getter方法. --> Test 测试类: package com ...

  7. A candidate solution for Java Web Application - current session

    Motivation Do it once, resue for ever. Audience myself, Java Web developers Scope 应用案例 图书借阅系统 阶段1需求: ...

  8. bootstrap-5

    代码(一) 在bootstrap中主要提供了三种代码风格:详见688行-730行 1.使用<code></code>来显示单行内联代码 2.使用<pre></ ...

  9. Android遇到的错误记录

    解决小米手机无法收到开机广播的问题 http://blog.csdn.net/ksr12333/article/details/16116627 怎样在Android Studio中打开DDMS窗口? ...

  10. Uncaught TypeError: _react2.default.findDOMNode is not a function

    react 查找某节点时报错 Uncaught TypeError: _react2.default.findDOMNode is not a function 代码: import React, { ...