How not to abort http response c#

前端 未结 8 696
刺人心
刺人心 2020-12-18 03:34

I need to run several methods after sending file to a user for a download. What happens is that after I send a file to a user, response is aborted and I can no longer do any

8条回答
  •  既然无缘
    2020-12-18 03:58

    Update
    Since you said no second page, do this instead. Add a section to your page that checks for a query string parameter (something like fileid, or path, etc...). If this value is present then it initiates the download process using your existing code. If this value is not present then it runs like normal.

    Now when the user clicks the download link you perform a post back (which you are already doing). In this post back create an iFrame on the page and set the URL of the iFrame to your pages URL with the added query string parameter (mypage.aspx?id=12664 or ?download=true, something like that). After creating the iframe perform what ever additional databinds/etc... you wish too.

    Example
    - http://encosia.com/ajax-file-downloads-and-iframes/

    This above linked example uses an iFrame and an update panel, just like you are talking about.

    Original Post
    Response.Flush will allow you to continue processing after you send the file to the user, or just don't call Response.End (you don't really need too).

    However Daniel A. White is correct, you can't actually redirect from your code after you send a file, you will get an error if you try. BUT you can continue to perform other server side operations if you need to.

    Other answers agree with the general consensus, you can't redirect after a file starts downloading: https://stackoverflow.com/a/822732/328968 (PHP, but same concepts since it involves HTTP in general). or Directing to a new page after downloading a file.

提交回复
热议问题