Solved

Cannot redirect after HTTP headers have been sent.

Posted on 2006-06-20
10
3,144 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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