how to redirect to another page after response.end()? [Resolved]

Posted by Hemanth.Gottipati under ASP.NET on 4/30/2013 | Points: 10 | Views : 19836 | Status : [Member] | Replies : 19
Hi,
I want to redirect to another page after response.end()?
this is my code pls help me?
LocalReport rep = new LocalReport();
rep.ReportPath = "emrReports/prescription.rdlc";
ReportDataSource dsPrescription = new ReportDataSource("dsPrescription1", objRepMedicationDAO.getPrescription(objrepMedicationDTO).Tables[0]);
rep.DataSources.Clear();
rep.DataSources.Add(dsPrescription);
//objPrintReport.Export(rep);
//rep.Refresh();
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

byte[] bytes = rep.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);
// FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Pdf/" + Session["PatientId"].ToString() + ".pdf"), FileMode.OpenOrCreate, FileAccess.Write);
FileStream fs = new FileStream(Server.MapPath(@"~\test.pdf"), FileMode.Create);

fs.Write(bytes, 0, bytes.Length);
fs.Close();

string path = Server.MapPath(@"~\test.pdf");



// System.Diagnostics.Process.Start(path);
System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server


if (file.Exists) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";

Response.WriteFile(file.FullName);
Response.End();
Response.redirect("download.aspx");

}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}

this will not working...if comment response.end(), redirect to page but pdf not opened..pls suggest me




Responses

Posted by: Hemanth.Gottipati on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down

Resolved
Hi santhi very very thanks for replies and suggest ur ideas..
Finally i got the solution....first save all the pdfs in project folder..and get the particular pdf url and open in the another brwoser tab..
and below is the code
 Warning[] warnings;

string[] streamids;
string mimeType;
string encoding;
string extension;

byte[] bytes = rep.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Pdf/" + Session["Id"].ToString() + ".pdf"), FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(bytes, 0, bytes.Length);
fs.Close();

DirectoryInfo di = new DirectoryInfo(System.Web.Hosting.HostingEnvironment.MapPath("~/Pdf"));
FileInfo[] TXTFiles = di.GetFiles("*.pdf");
if (TXTFiles.Length > 0)
{
bool imageurl = false;

foreach (FileInfo file in TXTFiles)
{

if (file.ToString() != Session["Id"] + ".pdf")
{

imageurl = false;
}
else
{
imageurl = true;
break;

}
}
if (imageurl == true)
{
Response.Write("<script>");
Response.Write("window.open('../Pdf/" + Session["Id"] + ".pdf', '_newtab');");
Response.Write("</script>");
Response.Write("<script language='javascript'>window.location='page2.aspx';</script>");
}

}



}


Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

You cannot do that..
Because the reposne has end..
Actually the server is responding for your request right..
I've tried this same thing... atlast i understood the concept..
Response End : The server will stop responding your request..

Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Atlast i have done like this.. actually i will redirect to the page called page2.aspx from page1.aspx..
and i will download the file from the page2.aspx.. I dont think so this is the correct way..
But i had done like this before 6 months in my project...
There i got the requirement like this..

Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Wat i suggested would work...
I had a scenario like user would select a file from the list of the files available say page called page1.aspx..
The user needs to download the file once he clicks on the links..
After the download , it has to redirect to a page say page2.aspx...

I would redirect first once the user clicked on the file name in page1.aspx to page2.aspx..
I will get the file name in the session value wat the user has clicked in page1.aspx..
I will do the download process in the page2.aspx in the page_load function..

Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naresh_V on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
No need to do anything man. You don't have option to open the pdf in same page itself. Take another .aspx page just redirect to that page and onload do your stuff as
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";

Response.WriteFile(file.FullName);
Response.End();
Response.redirect("download.aspx");

Naresh Veginati

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi thanks for reply can you please send the example code?


Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi thanks for replay,can you please send example code?

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 4/30/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,i am assigning path to session variable and write the download logic in page2.aspx but it will be downloaded in page1.aspx can you pls suggest me?

Page1.aspx:
LocalReport rep = new LocalReport();
rep.ReportPath = "emrReports/prescription.rdlc";
ReportDataSource dsPrescription = new ReportDataSource("dsPrescription1", objRepMedicationDAO.getPrescription(objrepMedicationDTO).Tables[0]);
rep.DataSources.Clear();
rep.DataSources.Add(dsPrescription);
//objPrintReport.Export(rep);
//rep.Refresh();
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;

byte[] bytes = rep.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);
// FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Pdf/" + Session["PatientId"].ToString() + ".pdf"), FileMode.OpenOrCreate, FileAccess.Write);
FileStream fs = new FileStream(Server.MapPath(@"~\Prescription.pdf"), FileMode.Create);

fs.Write(bytes, 0, bytes.Length);
Session["fs"] = Server.MapPath(@"~\Prescription.pdf");

fs.Close();

page2.aspx: Page_load event
if (Session["fs"] != null)
{
string path = Server.MapPath(@"~\Prescription.pdf");
System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server


if (file.Exists) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";

Response.WriteFile(file.FullName);
Response.End();


}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}




Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

whether the file has to download in the page1.aspx or page2.aspx..
According to the logic it will be downloaded in the page2.aspx..


Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
I want to download either page1 or page2 but if download in page1 the page will be redirected to page2 or after redirecting page2 it will be downloaded
any scenario i have no problem..i am writing logic in page2 but it will be downloaded in page1 and it will not be redirected to page2
pls suggest me

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

It works ...
If you write the logic in page2.aspx.. it will be downloaded in page2.aspx..
Try it..
I will post my sample logic code here...

Page1.aspx
<asp:Button ID="Button1" runat ="server" OnClick="Button1_Click" >

Onclick Event in Page1.aspx:

Response.Redirect("Page2.aspx")

Page2.aspx:

Response.Clear();
Response.ContentType = "application/pdf";

Response.WriteFile(Server.MapPath("filepath")); //filepath...
Response.End();



Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi thanks for reply..
i am writing also same logic and i am posted above my code in that code after session["fs"] i will write response.redirect
after that in page2.aspx page_load event i will write the download logic but it will down load in page1.aspx only
pls suggest me



Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Is it???????
DId you try the above one??????????????
It will be downloaded in the page2.aspx...
Can you attach the pages???????

Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
yes sure....again i will send the logic also and also screen shots of the my application...pls once check it and given suggestion

Page1.aspx:
             Warning[] warnings;

string[] streamids;
string mimeType;
string encoding;
string extension;

byte[] bytes = rep.Render(
"PDF", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);

FileStream fs = new FileStream(Server.MapPath(@"~\Prescription.pdf"), FileMode.Create);

fs.Write(bytes, 0, bytes.Length);
Session["fs"] = Server.MapPath(@"~\Prescription.pdf");
fs.Close();
response.redirect("page2.aspx");


Page2.aspx:(page_load event)
           if(session["fs"]!=null)

{
string path = Server.MapPath(@"~\Prescription.pdf");
System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server


if (file.Exists) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";

Response.WriteFile(file.FullName);
Response.End();
}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}


Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Let me try your code..

Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanth.Gottipati on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
yes i am already check with this code..but the file will be downloaded in page1.aspx...it will not be redirected
after response.redirect the debbugging will be entered in page2.aspx page_load event..
In that the above logic will be executed and it will downloaded in pag1.aspx only...
I dont know why it will not download in page2.aspx..past 3days i am working in these scenario only but no output..........

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naresh_V on: 5/2/2013 [Member] Starter | Points: 25

Up
0
Down
http://encosia.com/ajax-file-downloads-and-iframes/
use above link.
but you need to do one more thing. in iframe.js add small code.
add small code like this.
document.body.appendChild(iframe);
after this write this function. that's it. do what ever u want in that function....
setTimeout(function () { window.location = "www.google.com"; }, 3000);

The problem with response.end is it will top the asp page life cycle and send the response to browser so can't perform any actions after response.end.


Naresh Veginati

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 5/3/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Yeah, that's good...
:)

Thanks & Regards,
Santhi .V

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mmmm on: 5/3/2013 [Member] Starter | Points: 25

Up
0
Down
nbmbnmbcmvm

kjhkg

fdsf

Hemanth.Gottipati, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response