Solved

Cannot redirect after HTTP headers have been sent.

Posted on 2006-06-20
10
3,164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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.…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

752 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