【问题标题】:why Response.End Not working?为什么 Response.End 不工作?
【发布时间】:2016-01-04 08:06:27
【问题描述】:

可能是一个重复的问题。但我的情况有所不同。我有一个页面来下载文本文件。在页面中,我首先将文本文件解密为string plainText。然后将该字符串写入文件并上传到名为 Decrypted Files 的文件夹。下载解密文件并删除以前保存的文件。

这是我的下载代码

                //Write the decrypted text to folder in the server.
                System.IO.File.WriteAllText(Server.MapPath("~/Decrypted Files/" + FileName), plainText);

                //Code for Download
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename='" + FileName + "'");
                Response.WriteFile(Server.MapPath("~/Decrypted Files/" + FileName));

                //Delete File from the folder
                if (System.IO.File.Exists(Server.MapPath("~/Decrypted Files/" + FileName)))
                {
                    System.IO.File.Delete((Server.MapPath("~/Decrypted Files/" + FileName)));
                }

                Response.End();

但是代码执行不会从Response.End(); 继续,并且 .aspx 页面没有完成加载。我的代码有什么问题?

【问题讨论】:

  • 为什么不运行项目并启用异常设置/设置调试点?问题可能来自其他行而不是 Response.End。也看到这个答案 - stackoverflow.com/a/18477989/2010289
  • "The code execution not continue from Response.End()" - 你能澄清一下这句话的意思吗?显然你知道Response.End 会抛出 ThreadAbortException,所以你不希望代码 那行之后执行......那么你在说什么代码?
  • 改用 Response.TransmitFile

标签: c# asp.net download httpresponse


【解决方案1】:

现在我知道我的代码有什么问题。在结束之前我删除了文件。所以我将代码更改如下,一切正常。

                //Write the decrypted text to folder in the server.
                System.IO.File.WriteAllText(Server.MapPath("~/Decrypted Files/" + FileName), plainText);

                //Code for Download
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename='" + FileName + "'");
                Response.WriteFile(Server.MapPath("~/Decrypted Files/" + FileName));
                Response.Flush();

                //Delete File from the folder
                if (System.IO.File.Exists(Server.MapPath("~/Decrypted Files/" + FileName)))
                {
                    System.IO.File.Delete((Server.MapPath("~/Decrypted Files/" + FileName)));
                }
                HttpContext.Current.Response.End();

【讨论】:

    【解决方案2】:

    将该按钮(用于导出)放在更新面板中

    【讨论】:

    • (这篇文章似乎没有为问题提供quality answer。请编辑您的答案,或者将其作为对问题的评论发布)。
    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 2018-02-23
    • 2016-11-19
    • 1970-01-01
    • 2015-06-15
    • 2013-06-01
    • 2011-09-29
    • 2014-05-16
    相关资源
    最近更新 更多