Solved

Cannot redirect after HTTP headers have been sent.

Posted on 2006-06-20
10
3,115 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:vkarumbaiah
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Assisted Solution

by:whityum
whityum earned 20 total points
ID: 16944967
that is true, you cannot send another header after parts of the page are sent.  you could try writing javascript to the page or http meta redirects to the page.
0
 
LVL 12

Accepted Solution

by:
fruhj earned 200 total points
ID: 16945033
Hi vkarumbaiah,

  The short answer is, you cannot do this with asp.net
  When you return file content the way you do, that's seen as download by IE.

  There are a few approaches you could take with javascript - one is to launch a new window, to a new URL where you place the above code - the url would return the PDF - since it's in a new window - your old window is still open.

  Part of the javascript that launches the new window could also redirect the old window to a "thank you" page.
  I suppose with a little more knowledge of the browser, there might be an event you could trap for that would let you know when the download finished.

 

Thanks!
0
 
LVL 1

Author Comment

by:vkarumbaiah
ID: 16945484
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




0
 
LVL 12

Expert Comment

by:fruhj
ID: 16945621
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.
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16950532
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.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:vkarumbaiah
ID: 16952505
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.

0
 
LVL 15

Assisted Solution

by:GavinMannion
GavinMannion earned 30 total points
ID: 16957489
Yeah unfortunately this is a common complaint about web development, basically it boils down to the web being a stateless environment where we lose control over what our users are doing...

The best we can do is make it obvious and easy but unfortunately we cannot control the user like we can in a windows environment.

You will notice all major download sites don't actually redirect you automatically after the download has completed/started.

You get redirected to a page which says something along the lines of... "Your download should start automatically if nto please click this link." Then just after that they say something along the lines of "Click here to return to where you were."

0
 
LVL 1

Author Comment

by:vkarumbaiah
ID: 16958849
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.


0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16959652
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....
0
 
LVL 1

Author Comment

by:vkarumbaiah
ID: 17082572
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now