C#中使用Response下载
2024-10-21 18:57:29
正常流程
正常的从服务器端下载文件的流程
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.ContentType = "application/ms-download";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
调试
对于最后一句代码HttpContext.Current.Response.End();
此语句执行后 后面的代码就不会被执行。
查看Log日志中记录的Exception的信息,显示HttpContext.Current.Response.End()这条语句正在停止进程,也就是不会再执行后面的代码。
总结
如果在调用该方法后还有要执行的代码,就注释掉最后一句代码。
即为:
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.ContentType = "application/ms-download";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
//注释该行代码
//HttpContext.Current.Response.End();
最新文章
- python学习之深入
- FragmentStatePageradapter 与 FragmentPageradapter的区别
- October 11th 2016 Week 42nd Tuesday
- Why we need model on Django ?
- class属性中为什会添加非样式的属性值?
- python(五)图形用户界面easyGUI入门
- (IOS)签名Demo
- 去除android或者iOS系统默认的一些样式总结
- 如何去掉(隐藏)系统的StatusBar(状态栏)
- 【interview】Microsoft面经
- mshcMigrate制作的mshc文件中有链接打不开
- 设计模式(三)Singleton Pattern单例设计模式
- JFinal3.0 sql管理与动态生成
- 利用ML&;AI判定未知恶意程序——里面提到ssl恶意加密流检测使用N个payload CNN + 字节分布包长等特征综合判定
- Unix考古记:一个“遗失”的shell
- LeetCode: Set Matrix Zeroes 解题报告
- MapReduce学习
- 【有上下界网络流】【ZOJ】2314 Reactor Cooling
- qt程序运行时的错误error:undefined reference to `_imp___ZN10QTcpSocketD1Ev'
- sql中where以后and和or的用法
热门文章
- C语言初级编程实践:2048小游戏
- 【mq读书笔记】定时消息
- [BUGCASE]Webpack打包报JavaScript堆内存泄漏的错误
- [整理]qbxt集训10场考试 大 杂 烩 (前篇)
- EF Core 执行SQL语句和存储过程
- k8s实验操作记录文档
- 第八章 Python类中常用的特殊变量和方法
- 第11.15节 Python正则表达式转义符定义的特殊序列
- PyQt(Python+Qt)学习随笔:Designer中QDialogButtonBox确认clicked信号是哪个按钮发送的方法
- buu学习记录(下)(做题是不可能做题的)