为什么 Response.End 不工作?

why Response.End Not working?

可能是一个重复的问题。但我的情况有所不同。我有一个页面可以下载文本文件。在页面中,我首先将文本文件解密为 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 页面没有完成加载。我的代码有什么问题?

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

                //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();

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