为什么Response.End不工作?

时间:2016-01-04 08:06:28

标签: c# asp.net download httpresponse

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

2 个答案:

答案 0 :(得分:2)

现在我的代码出了问题。在结束之前我删除了文件。所以我改变了代码如下,一切正常。

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

答案 1 :(得分:-1)

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