Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can we call response.redirect after Response.TrsnsmitFile
Posted
Updated 6-Jun-17 19:33pm
Comments
Prasad Khandekar 13-Jun-13 3:07am    
Hello,

Unfortunately you can not call Redirect after TransmitFile. According to the MSDN documentation for Response.Redirect(string url), it will throw an HttpException when "a redirection is attempted after the HTTP headers have been sent". Since Response.Redirect(string url) uses the Http "Location" response header (http://en.wikipedia.org/wiki/HTTP_headers#Responses), calling it will cause the headers to be sent to the client. This means that if you call it a second time, or if you call it after you've caused the headers to be sent in some other way, you'll get the HttpException.

What you can instead do is to redirect to a page with meta-refresh tag (http://en.wikipedia.org/wiki/Meta_refresh) in the <head></head> section, pointing to the download url. This way the redirected page would show up in the browser first and then after a while file will get downloaded.

e.g. <meta http-equiv="refresh" content="5; url=http://example.com/getFile?id=537373">

Regards,
mahakaal 13-Jun-13 4:18am    
Thanx Prasad for your sharing valuable info...
Please check my answer.
mahakaal 13-Jun-13 8:52am    
Tadit, thanks as your suggestion has worked for me.. but would you help me if i will try to solve the same issue with different approach...
Most welcome. So, what is the different approach? Explain.

1 solution

Problem

  • The Response.Redirect() before the Response.End() will redirect, but the file is never going to be generated.
  • If you put the Response.Redirect() after the Response.End(), the file will be generated, but it will not redirect.


Solutions


  1. Showing the thank you message at the beginning of the file generation.
    Users are used to this anyway, and you can word it like: “Thanks. Your download will start in a second”. This is the most modern approach.
  2. Refer - Redirecting to another page after Response.End() has been called in C#[^]
    Quote:
    I suggest to add a redirect header. Something like this:
    C#
    Response.AddHeader("Refresh", "3; url=index.html");

    Where 3 is time delay and index.html is url you need to redirect to

 
Share this answer
 
v2
Comments
mahakaal 19-Jun-13 0:47am    
Tadit, the solution you gave me is working but not on all internet browsers.
Which solution you tried? Second one?
In which browser it is not working?
mahakaal 19-Jun-13 1:10am    
either in chrome or firefox..
i had tried the second one.
Ok, then go for Solution 1, which is the most popular and modern one.
mahakaal 19-Jun-13 6:34am    
Talit, i haven't try the 1st fmethod. but after trying the second method. i thought of doing this with the help of timer. which is serving my purpose. but i would want your suggestion on that. Below is the logic i have used for it.

on submit button
1. i am saving information into database
2. Saving the jobno into session variable session["temp_jobno"] and then reloading the page
3. on page load event if session["temp_jobno"] value is not null than calling a timer with interval of 1 sec.
4. on tick event of timer i am saving session value into a string variable(which is acutally jobno) and setting session["temp_jobno"]=null as well as disabling the timer control then sending the pdf to browser for dowload.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900