Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

asp.net - How not to abort http response c#

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 anything after response.end().

for example, this is my sample code:

 Response.Clear();
 Response.AddHeader("content-disposition", "attachment;  filename=test.pdf");
 Response.ContentType = "application/pdf";
 byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
 Response.BinaryWrite(a);
 Response.End();
 StartNextMethod();
 Response.Redirect(URL);

So, in this example StartNextMethod and Response.Redirect are not executing.

What I tried is I created a separate handler(ashx) with the following code:

public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.AddHeader("content-disposition", "attachment;  filename=test.pdf");
            context.Response.ContentType = "application/pdf";
            byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
            context.Response.BinaryWrite(a);
            context.Response.End();
        }

and call it like this:

Download d = new Download();
d.ProcessRequest(HttpContext.Current);
StartNextMethod();
Response.Redirect(URL);

but the same error happen. I've tryied to replace Response.End with CompleteRequest but it doesn't help.

I guess the problem is that I'm using HttpContext.Current but should use a separate response stream. Is that correct? how do I do that in a separate method generically (Assume that I want my handler to accept byte array of data and content type and be downloadable from a separate response. I really do not want to use a separate page for a response.

UPDATE
I still didn't find a good solution. I'd like to do some actions after user has downloaded a file, but without using a separate page for a response equest thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...