Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot redirect after HTTP headers have been sent.

Posted on 2006-06-20
10
Medium Priority
?
3,282 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 60 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 600 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
Technology Partners: 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!

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

782 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