vkarumbaiah
asked on
Cannot redirect after HTTP headers have been sent.
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("Con tent-Lengt h", Fi.Length.ToString())
Response.AppendHeader("con tent-dispo sition", "attachment; filename=MyFile.pdf" )
Response.WriteFile("c:\MyF ile.pdf", 0, Fi.Length)
Response.Flush()
Response.Redirect("http://www.MyWebsite.com",false )
Catch ex As Exception
ReportError(ex)
Finally
End Try
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("Con
Response.AppendHeader("con
Response.WriteFile("c:\MyF
Response.Flush()
Response.Redirect("http://www.MyWebsite.com",false
Catch ex As Exception
ReportError(ex)
Finally
End Try
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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.
ASKER
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.
But what if the user clicks that button before clicking the download button (even if it was by accident) , I would have erroneus data.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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....
It looks like Cnet use javascript to get the download to start but I don't know how to do this....
ASKER
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.locatio n='"+desti nation+"'" ,4000);
window.location.replace=de stination;
}
//-->
</script>
The page redirects to the thankyou.aspx page in 4 seconds after the button has been clicked.
If Not Page.IsPostBack Then
btnDownload.Attributes.Add
End If
Script to add to the page source
<script language="JavaScript" type="text/javascript">
<!--
function SetUpRedirect()
{
var destination = "thankyou.aspx";
setTimeout("window.locatio
window.location.replace=de
}
//-->
</script>
The page redirects to the thankyou.aspx page in 4 seconds after the button has been clicked.
ASKER
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