Solved

Cannot redirect after HTTP headers have been sent.

Posted on 2006-06-20
10
3,195 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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