如何使代码在Response.end之后执行 [英] how to make code execute after Response.end

查看:80
本文介绍了如何使代码在Response.end之后执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码就是这样

HttpContext.Current.Response.Clear();
     HttpContext.Current.Response.ContentType = "application/pdf";
     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "name" + ".pdf");
     HttpContext.Current.Response.TransmitFile("~/media/pdf/name.pdf");
     HttpContext.Current.Response.End();
     if (FileExists("/media/pdf/name.pdf"))
     {
         System.IO.File.Delete("D:/Projects/09-05-2013/httpdocs/media/pdf/name.pdf");
     }

我要在浏览器中下载name.pdf,下载后我要删除该文件.但是代码执行会在

Here I want to download name.pdf in the browser, and after the download I want o delete that file.But the code execution stops at

HttpContext.Current.Response.End();

该行之后没有代码.因此我的删除功能不起作用.是否可以解决此问题?

no code after that line is executed.so my delete function is not working.Is there any work around for this issue?

推荐答案

HttpResponse.End (根据

HttpResponse.End (as per documentation) raises a ThreadAbortException and as you do no attempt to handle this your method exits.

我不确定为什么必须使用End(),但是可以将清理"代码放在finally语句中.

I'm not sure exactly why you must use End(), but you could put the "cleanup" code in a finally statement.

这篇关于如何使代码在Response.end之后执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆