Link to home
Start Free TrialLog in
Avatar of vkarumbaiah
vkarumbaiahFlag for United States of America

asked on

Cannot redirect after HTTP headers have been sent.

Hi
I have an ASP.Net page where a visitor enters some information and clicks a button to downlod a pdf file.

Once the file has been downloaded I want to redirect the visitor to another website.
 
The file downloads fine, no problems there, however  when I get to the Response.Redirect I get the error "Cannot redirect after HTTP headers have been sent." .
I ofcourse cannot use server.transfer since I am redirecting to a page that is not on the same server.

here is my code  any suggestions anbody?

Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
            Try
                    Dim Fi As FileInfo = New FileInfo("c:\MyFile.pdf")

                   Response.Clear()
                    Response.ContentType = "application/pdf"
                    Response.AppendHeader("Content-Length", Fi.Length.ToString())
                    Response.AppendHeader("content-disposition", "attachment; filename=MyFile.pdf" )
                    Response.WriteFile("c:\MyFile.pdf", 0, Fi.Length)
                    Response.Flush()
                   
                   Response.Redirect("http://www.MyWebsite.com",false)

        Catch ex As Exception
             ReportError(ex)
        Finally

        End Try

SOLUTION
Avatar of whityum
whityum

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vkarumbaiah

ASKER

Hi fruhj
I am not sure there is a download complete event like you are suggesting. If you know of any resource where I can read about this can you please forward it to me?

My knowledge javascript isnt exactly the best could you give me some sample code on how you suggest I should achieve what you are suggesting?

Thank you for your help




Avatar of fruhj
fruhj

Unfortunately, I'm not pro at Javascript either.

If I had to do this formyself, I would try and find some dowload sites like cnet's download.com or tucows.com etc.. to see if I could find a similar behavior - if so then it would just be a matter of looking at the javascript they use.
You cannot accurately tell when the download has completed. The easiest way would be to open a new window which calls the download code as you have, on this page put a button that says click here when download completes. Then close the page when that button it pushed.

Interesting Idea GavinMannion
But what if the user clicks that button before clicking the download button (even if it was by accident) , I would have erroneus data.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
True, I took a look at a couple of sites like Cnet's download.com as fruhj had suggested and even the microsoft downloads page and they all do that. Im not averse to doing the same however I just dont know how to do it. Id greatly appreciate any sample code or a link to a resource that describes how to do this.


The one way you can do this is by putting an iFrame in the page and getting it to force the download in codebehind.

It looks like Cnet use javascript to get the download to start but I don't know how to do this....
Add to the download button click

      If Not Page.IsPostBack Then
            btnDownload.Attributes.Add("onclick", "SetUpRedirect()")
        End If

Script to add to the page source
 
  <script language="JavaScript" type="text/javascript">
    <!--
        function SetUpRedirect()
           {
                var destination = "thankyou.aspx";                                  
                setTimeout("window.location='"+destination+"'",4000);
                window.location.replace=destination;                            
               
              }
    //-->
    </script>

The page redirects to the thankyou.aspx page in 4 seconds after the button has been clicked.