Response.TransmitFile和发送后,删除 [英] Response.TransmitFile and delete it after transmission

查看:327
本文介绍了Response.TransmitFile和发送后,删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现我的网站GEDCOM出口。

I have to implement GEDCOM export in my site.

我的.NET code。在服务器上创建一个文件GEDCOM点击导出时。

My .net code created one file at server when export to gedcom clicked.

然后,我需要从服务器下载到客户端的,以及用户应问在哪里保存该文件,则需要savedialog意思。

Then I need to download it to client from server, as well as user should be asked where to save that file, meaning savedialog is required.

之后,我想删除服务器文件。

After it's downloaded, I want to delete that file from server.

我得到了一个code传输从服务器上的文件到客户端:

I got one code to transmit file from server to client:

Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.TransmitFile(Server.MapPath("~/" + FileName));
Response.End();

链接

但我不能这样code作为到Response.End 后删除文件结束反应,使行之后写什么code未执行。

but I am not able to delete the file after this code as Response.End ends response so whatever code written after that line is not execute.

如果我做code之前到Response.End()删除文件; ,然后文件不发送,我得到一个错误

If I do code to delete file before Response.End();, then file does not transmitted and I get an error.

推荐答案

任何你到Response.End穿上后不会得到执行,因为它抛出一个ThreadAbortException在该点停止页面的执行。

Anything you put after Response.End won't get executed because it throws a ThreadAbortException to stop execution of the page at that point.

试试这个:

string responseFile = Server.MapPath("~/" + FileName);

try{
    Response.ContentType = "text/xml";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
    Response.TransmitFile(responseFile);
    Response.Flush();
}
finally {
    File.Delete(responseFile);
}

这篇关于Response.TransmitFile和发送后,删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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