I would like to download a .CSV and redirect the user to another page.
This is my code
protected void btnExport_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=" + Request.QueryString["exportName"] + ".csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/ms-excel";
//CODE TO WRITE CSV
List<Company> companies = (List<Company>)Session["SelectedCompanies"];
foreach (Company company in companies)
{
HttpContext.Current.Response.Write(company.Name + ";");
HttpContext.Current.Response.Write(Environment.NewLine);
}
HttpContext.Current.Response.Redirect("otherpage.aspx", true);
}
But unfortunatly, it does only the redirect and not the download of the .CSV.
And if I replace HttpContext.Current.Response.Redirect("otherpage.aspx", true); by HttpContext.Current.Response.End();, then it does only the download of the .CSV and not the redirect.
But I would like to have both.
With such cases, what I do is :
have an iframe hidden
< iframe id=”DownloadIFrame” visible=”false” width=”1″ height=”1″ style=”display:none” >
< /iframe >
give this Iframe the url of my downlood (like download.ashx) while changing the content of my page.
On the server
this way the Iframe hit the download script on the server and gives back the file to be downloaded by the client. The client at the same time sees the content of its page changed and is happy..
good luck,