locked
Adding Code after calling Response.TransmitFile doesn't Execute RRS feed

  • Question

  • User248267340 posted

    I have a small routine to upload a file, modify it, then download it.

    It appears that after using the class "Response" any code after it doesn't get executed.

    Is there a way to overcome that?

    When the routine prompts end-user to download file, I'd like to delete it afterwards. It seems I can't do that.

    Saturday, May 22, 2021 12:04 AM

Answers

  • User409696431 posted

    Post the code (the smallest sample that will illustrate the problem) where you use Response.  The answer to your question depends on it.

    For example, if you are calling

    HttpContext.Current.Response.End();
    or
    HttpContext.Current.Response.Close():

    before you try to delete the file, don't do that.  Just call

    HttpContext.Current.Response.Flush(); 

    instead and see if that solves your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 23, 2021 12:16 AM

All replies

  • User409696431 posted

    Post the code (the smallest sample that will illustrate the problem) where you use Response.  The answer to your question depends on it.

    For example, if you are calling

    HttpContext.Current.Response.End();
    or
    HttpContext.Current.Response.Close():

    before you try to delete the file, don't do that.  Just call

    HttpContext.Current.Response.Flush(); 

    instead and see if that solves your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 23, 2021 12:16 AM
  • User248267340 posted

    Thanks Kathy, for your assistance. I added a line to flush, but it didn't help. Here's my code:

                if (FileUpload1.HasFile)
                {
                    myServerPath = Server.MapPath(FileUpload1.FileName);
                    string myClientPath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
                    FileUpload1.SaveAs(myServerPath);
    
                    string mydir = Path.GetDirectoryName(myServerPath);                         
                    wfilename = Path.GetFileNameWithoutExtension(myServerPath);                 // Save just the filename
                    wext = Path.GetExtension(myServerPath);                                     // Save just the extension
    
                    updatedfilename = wfilename + "_Updated" + wext;
                    WorkFile = Path.Combine(mydir, updatedfilename);
    
                    Fix_the_Data(myServerPath, WorkFile);					    // Fix the file contents.
                    File.Delete(myServerPath);
    
                    string downfile = "~/MyTempFolder/" + updatedfilename;              
                    string arg1 = "Content-Disposition";
                    string arg2 = "attachment; filename=" + updatedfilename;
    
                    Response.ContentType = "text/plain";
                    //Response.AppendHeader("Content-Disposition", "attachment; filename=Test.txt");  	
                    Response.AppendHeader(arg1, arg2);
                    Response.TransmitFile(Server.MapPath(downfile));
                    Response.End();
    		Response.Flush();
    
    		File.Delete(WorkFile);				// This line doesn't get executed.
                }

    Monday, May 24, 2021 7:30 PM
  • User409696431 posted

    Please look at my response again, and follow it. 

    "For example, if you are calling

    HttpContext.Current.Response.End();
    or
    HttpContext.Current.Response.Close():

    before you try to delete the file, don't do that.  Just call

    HttpContext.Current.Response.Flush(); "

    Again, don't use Response.End().  Just use Response.Flush().

    Monday, May 24, 2021 8:52 PM
  • User248267340 posted

    I see. I Interpreted your statement differently.

    Thank you so very much - that is the perfect solution.

    Monday, May 24, 2021 10:20 PM