2

May be it is a Duplicate question. But my case is something different. I have a page to download the text file. In the page first i decrypt the text file into string plainText. then write that string into a file and upload to a folder named as Decrypted Files. The download that decrypted file and delete previously saved file.

here is my code for download

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

But The code execution not continue from Response.End(); and the .aspx page not finishing its loading. What is wrong with my code ?

Ritz
  • 394
  • 1
  • 3
  • 12
  • Why not run the project and enable Exception settings/set debug points? The problem can be from other lines instead of Response.End. See this answer too - http://stackoverflow.com/a/18477989/2010289 – kent-id Jan 04 '16 at 08:22
  • "The code execution not continue from Response.End()" - Can you please clarify what this sentence mean? Obviously you know that `Response.End` throws ThreadAbortException, so you are not expecting code *after* that line to execute... so what code you are talking about? – Alexei Levenkov Jan 04 '16 at 08:26
  • Try Response.TransmitFile instead – User2012384 Jan 04 '16 at 08:31

2 Answers2

2

Now i got what is wrong with my code. Before ending I deleted the file. So i changed the code as follows and everything working fine.

                //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();
Ritz
  • 394
  • 1
  • 3
  • 12
-1

put that button (which you are using to export) in update panel

  • (This post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer, or just post it as a comment to the question). – sɐunıɔןɐqɐp Jun 19 '18 at 08:35