locked
Why does my downloaded text file include HTML at the bottom? RRS feed

  • Question

  • User248267340 posted

    Using aspx, I learned how to prompt for a text file with FileUpload, and then modify the file contents on the server side, and then finally offer it for download.

    However, the file that is downloaded includes the html from my Default.aspx.

    Here's a snippet:

                    ModifyFileContents(myServerPath, WorkFile);       // This modifies the file on the Server side
    
                    File.Delete(myServerPath);
    
                    string downfile = "~/" + updatedfilename;               // Create virtual path -required for Response.TransmitFile
                    string arg1 = "Content-Disposition";
                    string arg2 = "attachment; filename=" + updatedfilename;
    
                    Response.ContentType = "text/plain";
                    Response.AppendHeader(arg1, arg2);
                    Response.TransmitFile(Server.MapPath(downfile));
                    Response.Flush();
    
                    File.Delete(WorkFile);

    Can you see anything there that I'm missing?

    Wednesday, May 26, 2021 12:08 AM

All replies

  • User248267340 posted

    I added an extra line after the last instruction (File Delete):

    Response.End;

    That seems to have cleared it up. Was that the right thing to do?

    Wednesday, May 26, 2021 12:22 AM
  • User-1330468790 posted

    Hi coreysan,

     

    I don't see errors in your current codes. However, from your problem description, it looks like some codes got executed after you transmited the file.

     

    I added an extra line after the last instruction (File Delete):

    Response.End;

    That seems to have cleared it up. Was that the right thing to do?

    It is correct and only way to guarantee no other future codes change or IIS filter will manipulate your response as intended.

     

    You can refer to the documentation for comparing the differences between Response.Flush() and Response.End().

     

    Best regards,

    Sean

    Wednesday, May 26, 2021 1:58 AM