PDF下载后重定向页面

我有一个aspx(比如1.aspx)页面,首先我下载一个pdf文件,然后我想重定向到一些Thanks.aspx页面。 代码是这样的:

protected void btnSubmit_Click(object sender, EventArgs e) { string pathId = string.Empty; if (Page.IsValid) { try { pathId = hidId.Value; DownloadPDF(pathId); Response.Redirect("Thanks.aspx"); } catch (Exception ex) { throw ex; } } } protected void DownloadPDF(string pathId) { if (!(string.IsNullOrEmpty(pathId))) { try { Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + pathId + ".pdf"); string path = ConfigurationManager.AppSettings["Pdf_Path"].ToString() + "\\" + pathId.Trim() + ".pdf"; Response.TransmitFile(path); } catch (Exception ex) { throw ex; } finally { HttpContext.Current.ApplicationInstance.CompleteRequest(); } } } 

问题是,文件保存对话框正常,我也可以下载该文件,但它没有被重定向到Thanks.aspx页面。

怎么解决这个?

我发现将PDF下载页面放在iframe中更容易。 这样,只需将iframe源指向PDF下载页面,即可在客户端激活PDF下载。 之后,您可以移动到新页面,或者只在页面中显示iframe,并在其中显示iframe。

如果刚刚下载了文件,则不进行预处理,您可以尝试以下操作:

 Response.AddHeader("Refresh", "12;URL=nextpage.aspx"); 

数字是刷新完成前的秒数:)

在HTTP中,请求只能有一个响应。 由于第一个响应是PDF文件,因此无法实现秒响应(即重定向)。

您可以尝试通过重定向到thanks.aspx来重新设计这两个页面,并让thanks.aspx自动开始下载。

Response.Redirect实际上向浏览器发送响应,该响应基本上表示此资源已移至其他URL。 但是,你也试图在响应中发送文件,所以这两件事可能会相互冲突。 尝试发回一些JavaScript,将它们发送到您要发送给它们的页面,而不是使用Response.Redirect。

 ScriptManager.RegisterStartupScript(Me, Me.GetType(), "redirectScript", "window.location.href='whateverurlhere.aspx';", True) 

请参阅此接受答案中提到的文章: https : //stackoverflow.com/a/11018277/1037864 (直接链接: http : //gruffcode.com/2010/10/28/detecting-the-file-download-dialog-在浏览器/ )

我们的想法是设置一个cookie并将其与文件一起发送。 同时,让等待页面在等待cookie到达时阻止UI。