c# - 如何让代码在 Response.end 之后执行

标签 c# asp.net

我的代码是这样的

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,下载后我想删除那个文件。但是代码执行停止在

HttpContext.Current.Response.End();

执行该行后没有代码。所以我的删除功能不起作用。是否有任何解决此问题的方法?

最佳答案

// Add headers for a csv file or whatever
Response.ContentType = "text/csv"
Response.AddHeader("Content-Disposition", "attachment;filename=report.csv")
Response.AddHeader("Pragma", "no-cache")
Response.AddHeader("Cache-Control", "no-cache")

// Write the data as binary from a unicode string
Dim buffer As Byte()
buffer = System.Text.Encoding.Unicode.GetBytes(csv)
Response.BinaryWrite(buffer)

// Sends the response buffer
Response.Flush()

// Prevents any other content from being sent to the browser
Response.SuppressContent = True

// Directs the thread to finish, bypassing additional processing
HttpContext.Current.ApplicationInstance.CompleteRequest()

关于c# - 如何让代码在 Response.end 之后执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16731745/

相关文章:

c# - 如何在服务器端捕获 ASP.NET Core 2 SignalR 异常并使用 JavaScript 在客户端处理它们?

c# - 检查表单是否已在 aspx 中提交

c# - 有什么可以模拟静态接口(interface)的吗?

c# - 以编程方式访问 Google Chrome 主页或起始页

javascript - 基于 html 的网站中 PC 的真正唯一标识

javascript - ASP.NET MVC - 更改选项卡 AJAX RenderAction

c# - 我是否需要维护计划来防止将来发生已知的 SQL Server 超时?

c# - 为什么在手指树和链表等中使用元组

c# - 将 Web 解决方案实现为 Winform

c# - 警报消息框